In MongoDB, the following data exist
{"_id": ObjectId ("59AF55078A8FC5E51FF425DE"),"title":"Title1","Col":"Col1", "Reader" : [ { "Readername" : "Jim", "Isread": True}, {"Readername" : "Kate" }, { "Readername" : "Lilei" } ], "Begindate" : "Wed Sep . . the: -: OneGMT+0800(China Standard Time)" }{"_id": ObjectId ("59AF552E8A8FC5E51FF425DF"),"title":"Title2","Col":"Col1", "Reader" : [ { "Readername" : "Jim" }, { "Readername" : "Kate" }, { "Readername" : "Lilei" }, { "Readername" : "Lily" } ], "Begindate" : "Wed Sep . . the: -: -gmt+0800(China Standard Time)" }{"_id": ObjectId ("59af55458a8fc5e51ff425e0"),"title":"Title3","Col":"Col1", "Reader" : [ { "Readername" : "Jim" }, { "Readername" : "Kate" } ], "begindate" : "Wed Sep . . the: Wu: -gmt+0800(China Standard Time)" }
Requirement 1: Query column is col1, and the reader is Lily's record:
> Db.articles.find ({col:'col1','Reader.readername':'Lily'})//Query Results{"_id": ObjectId ("59AF552E8A8FC5E51FF425DF"),"title":"Title2","Col":"Col1", "Reader" : [ { "Readername" : "Jim" }, { "Readername" : "Kate" }, { "Readername" : "Lilei" }, { "Readername" : "Lily" } ], "Begindate" : "Wed Sep . . the: -: -gmt+0800(China Standard Time)" }
That is, the objects in the array are made of shapes such as "Array name. Field"
Requirement 2: The title is Title2, and the reader is Lily's read record ' Isread ' set to True
> db.articles.update ({title:'Title2','Reader.readername':'Lily'},{$Set:{'Reader.$.isread': true})Writeresult ({"nmatched":1,"nupserted":0,"nmodified":1})
{"_id": ObjectId ("59AF552E8A8FC5E51FF425DF"), "title": "Title2", "col": "Col1", "reader": [{"Readername": "Jim"}, {"Readername": "Kate"}, {"Readername": "Lilei"}, {"Readername": "Lily", "Isread": true}], "Begindate": "Wed S EP 09:53:50 gmt+0800 (China Standard Time)}
The core is $, which can be understood as the array locator
83334129
Array object Query modification method in Mogodbshell