pymongo
pymongo使用集合
import datetime from pymongo import MongoClient from bson.objectid import ObjectId client = MongoClient() # 默认配置 db = client.footballnews collection = db.FootballnewsItem # 找一条 info = collection.find_one() # 通过objectid找 object_id = collection.find_one({"_id": ObjectId("5c18d00d137fb20fd4fdb0ef")}) # bulk inserts new_posts = [{"author": "Mike", "text": "Another post!", "tags": ["bulk", "insert"], "date": datetime.datetime(2009, 11, 12, 11, 14)}, {"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime.datetime(2009, 11, 10, 10, 45)}] result = collection.insert_many(new_posts) print(result.inserted_ids) # querying for more than one document for item in collection.find(): pass # just like we did with find_one() we can pass document to find() to limit return result for item in collection.find({"author": "Mike"}): print(item)
计数
count = collection.count_documents({}) count_mike = collection.count_documents({"author": "Mike"}) # range queries d = datetime.datetime(2009, 11, 12, 12) for item in collection.find({"date": {"$lt": d}}).sort("author"): print(item)

浙公网安备 33010602011771号