最近有需要用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({})