用Pymongo连接MongoDB取数

最近有需要用python从MongoDB上取数的需求,于是研究了一下Pymongo,在此做个小记。

from pymongo import MongoClient
client = MongoClient('mongodb://x.x.x.x:xxxxx')

db = client.[DATABASES_NAME]

print(db.collection_names())  # show the names of all the collections in selected database

collections = db[COLLECTION_NAME]

collections.find('{xxx:xxxxx,xxx:xxx}')  # Execute the query, which is equivalent to db.getCollection('xxx').find({})
collections.find_one  #same as db.getCollection('xxx').findOne({})

Contents


本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。

知识共享许可协议