MongoDB # $set Problems

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.