标签:
import pymongo client = pymongo.MongoClient('localhost', 27017)
或者可以这样
import pymongo client = MongoClient('mongodb://localhost:27017/')
db = client.mydb或者
db = client['mydb']
collection = db.my_collection或者
collection = db['my_collection']
db.collection_names()
collection.insert({"key1":"value1","key2","value2"})
全部删除
collection.remove()按条件删除
collection.remove({"key1":"value1"})
collection.update({"key1": "value1"}, {"$set": {"key2": "value2", "key3": "value3"}})
collection.find_one() collection.find_one({"key1":"value1"})查询多条记录:find()不带参数返回所有记录,带参数按条件查找返回
collection.find() collection.find({"key1":"value1"})
for item in collection.find(): print item
print collection.find().count()
collection.find().sort("key1") # 默认为升序 collection.find().sort("key1", pymongo.ASCENDING) # 升序 collection.find().sort("key1", pymongo.DESCENDING) # 降序
collection.find().sort([("key1", pymongo.ASCENDING), ("key2", pymongo.DESCENDING)])
标签:
原文地址:http://blog.csdn.net/shuaijiasanshao/article/details/51350881