mogodbshell中數組對象查詢修改方法
阿新 • • 發佈:2017-09-06
理解 定位 字段 color bsh begin objectid round obj
在mongodb中,存在如下數據
{ "_id" : ObjectId("59af55078a8fc5e51ff425de"), "title" : "title1", "col" : "col 1", "reader" : [ { "readername" : "jim", "isread" : true }, { "readername" : "ka te" }, { "readername" : "lilei" } ], "begindate" : "Wed Sep 06 2017 09:53:11 GMT +0800 (中國標準時間)" } { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09 :53:50 GMT+0800 (中國標準時間)" } { "_id" : ObjectId("59af55458a8fc5e51ff425e0"), "title" : "title3", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" } ], "beginda te" : "Wed Sep 06 2017 09:54:13 GMT+0800 (中國標準時間)" }
需求1:查詢欄目是col1,且讀者是lily的記錄:
> db.articles.find({col:‘col1‘,‘reader.readername‘:‘lily‘}) //查詢結果 { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09 :53:50 GMT+0800 (中國標準時間)" }
即數組中的對象用形如“數組名.字段”組成
需求2:把標題為title2,且讀者為lily的已讀記錄‘isread’設置為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" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W ed Sep 06 2017 09:53:50 GMT+0800 (中國標準時間)" }
核心是$,可以理解為數組定位器
83334129
mogodbshell中數組對象查詢修改方法