TIM*_*MEX 6 python mongodb pymongo
首先,查找是否存在文档匹配查询.
如果是,请使用新数据更新该文档.
否则,将新文档插入数据库.
Dhr*_*hak 10
您可以使用等于true的"upsert".然后使用"upsert"作为true运行的更新查询将完全按照您的要求执行.
来自MongoDb文档:
db.collection.update( criteria, objNew, upsert, multi )
Arguments:
criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, insert it
multi - if all documents matching criteria should be updated
Run Code Online (Sandbox Code Playgroud)
http://www.mongodb.org/display/DOCS/Updating
例:
db.test.update({"x": "42"}, {"$set": {"a": "21"}},True)
#True => Upsert is True
Run Code Online (Sandbox Code Playgroud)
请在此处查看"更新"文档:
http://api.mongodb.org/python/current/api/pymongo/collection.html