python连接mongodb密码认证实例

yipeiwu_com6年前Python基础

如下所示:

from pymongo import MongoClient
#建立和数据库系统的连接,指定host及port参数
client = MongoClient('localhost', 27017)
#连接mydb数据库,账号密码认证
db = client.mydb
db.authenticate("account", "password")
#连接表
collection = db.myset


#查看全部表名称
db.collection_names()
print db.collection_names()
 
#访问表的数据,过滤查询
item = collection.find({},{"name":1,"age":21})
for rows in item:
    print rows.values()
 
#访问表的一行数据
print collection.find_one()

以上这篇python连接mongodb密码认证实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

解决pyshp UnicodeDecodeError的问题

用最新版本(2.1.0)的pyshp解析shp文件的records时: records = sf.records() 如果records里面含有中文字段,那么就会报错: Uni...

pandas 数据实现行间计算的方法

如下所示: ###方法1:用shift函数,不用通过循环 import pandas as pd import numpy as np import matplotlib as p...

python机器学习之KNN分类算法

python机器学习之KNN分类算法

本文为大家分享了python机器学习之KNN分类算法,供大家参考,具体内容如下 1、KNN分类算法 KNN分类算法(K-Nearest-Neighbors Classification)...

python进程的状态、创建及使用方法详解

本文实例讲述了python进程的状态、创建及使用方法。分享给大家供大家参考,具体如下: 进程以及状态 1. 进程 程序:例如xxx.py这是程序,是一个静态的 进程:一个程序运行起来后,...

对python 中re.sub,replace(),strip()的区别详解

对python 中re.sub,replace(),strip()的区别详解

1.strip(): str.strip([chars]);去除字符串前面和后面的所有设置的字符串,默认为空格 chars -- 移除字符串头尾指定的字符序列。 st = " he...