python连接mongodb密码认证实例

yipeiwu_com7年前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设计】。

相关文章

python 判断三个数字中的最大值实例代码

python 判断三个数字中的最大值,具体代码如下所示: #判断三个数中最大值 n1= int(input('please enter the firest number:')) n...

Python定义一个跨越多行的字符串的多种方法小结

Python定义一个跨越多行的字符串的多种方法小结

方法一: >>> str1 = '''Le vent se lève, il faut tenter de vivre. 起风了,唯有努力生存。 (纵有疾风起,人...

python调用其他文件函数或类的示例

B.py调用A.py的函数或类 在同一个文件夹下 调用函数: A.py文件: def add(x,y): print('和为:%d'%(x+y)) B.py文件: im...

python脚本生成caffe train_list.txt的方法

首先给出代码: import os path = "/home/data//" path_exp = os.path.expanduser(path) classes =...

Python/ArcPy遍历指定目录中的MDB文件方法

如下所示: #遍历指定目录中的MDB文件,构造FeatureClass名 >>> target_folder = 'D:\T20161202' ... file_...