In MongoDB 2.4 and previous versions, Db.collections.update ({...},{"$set": {}}), or "Empty $set", can be performed normally. With Upsert and other parameters can be used in different convenient use.
however , after upgrading to MongoDB 2.6, the following error occurred when attempting to perform an empty $set operation due to the introduction of a strict parameter check.
>Use test #创建数据库switched to DB test
> Db.createcollection ('LWSLWS') #创建集合 {"OK": 1 }>Show Collections #查看集合lwslws> Db.lwslws.insert ({'name':'LWS'}) #插入数据WriteResult ({"ninserted": 1 })
>Db.lwslws.find () #查看数据 {"_id": ObjectId ("59cdcce01b4793371a9bb3b0"),"name":"LWS" }
#新增字段> Db.lwslws.update ({"_id": ObjectId ("59cdcce01b4793371a9bb3b0")},{'$set':{' Age': 25}}) Writeresult ({"nmatched": 1,"nupserted": 0,"nmodified": 1 })
#查看数据>Db.lwslws.find () {"_id": ObjectId ("59cdcce01b4793371a9bb3b0"),"name":"LWS"," Age": 25 }
#更新数据, but pass in a null value. > Db.lwslws.update ({"_id": ObjectId ("59cdcce01b4793371a9bb3b0")},{'$set': {}}) Writeresult ({"nmatched": 0,"nupserted": 0,"nmodified": 0,"Writeerror" : { "Code": 9, "errmsg":"' $set ' is empty. You must specify a field like so: {$set: {<field>: ...}}" }})
#Monkey patch Pymongo to allow empty $set fromPymongoImportcollectionpymongo_collection_update=collection. Collection.updatedefPymongo_collection_update_with_empty_set_support (self, spec, document, *args, * *Kwargs):if "$set" inchDocument anddocument["$set"] =={}: Document.pop ("$set") ifDocument = = {}: returnNonereturnPymongo_collection_update (self, spec, document, *args, * *Kwargs) collection. Collection.update= Pymongo_collection_update_with_empty_set_support
MongoDB # $set Problems