您的位置:首页 > 数据库 > Mongodb

Mongodb学习笔记 --- python读取mongodb数据

2017-09-28 16:08 483 查看
# -*- coding:utf-8 -*-
import pymongo
__author__ = 'yangxin'

class MongodbConn(object):

def __init__(self):
self.CONN = pymongo.MongoClient("ip", port)

def run(self):
database = "database_name"
db = self.CONN[database]
db.authenticate("username", "password")
col = db.collection_names()
# the col is datbases list
print col
# this is a database, in list loc 4
col = col[3]
collection = db.get_collection(col)
# query one document
document = collection.find_one()
print document
# query all document
documents = collection.find()
for i in documents:
# print key of (key: value)
print i.keys()

if __name__ == '__main__':
mongo_obj = MongodbConn()
mongo_obj.run()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: