mogodbshell中数组对象查询修改方法
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                mogodbshell中数组对象查询修改方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                在mongodb中,存在如下數(shù)據(jù)
{ "_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 (中國標準時間)" }即數(shù)組中的對象用形如“數(shù)組名.字段”組成
?
需求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 (中國標準時間)" }
核心是$,可以理解為數(shù)組定位器
需求3:刪除名為title2下的reader名字為lilei的讀者對象:
?
//未刪除前數(shù)據(jù) { "_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 (中國標準時間)" }//執(zhí)行命令 > db.articles.update({title:'title2','reader.readername':'lilei'},{$pull:{'reader':{readername:'lilei'}}})//執(zhí)行命令后查詢的結果 > db.articles.find({title:'title2'}); { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lily", "isread" : true } ], "begindate" : "Wed Sep 06 2017 09:53:50 GMT+ 0800 (中國標準時間)" }?
核心是用$pull命令查找數(shù)組名稱,然后通過屬性值刪除數(shù)組內(nèi)的對象記錄
?
轉(zhuǎn)載于:https://www.cnblogs.com/qkabcd/p/7483357.html
總結
以上是生活随笔為你收集整理的mogodbshell中数组对象查询修改方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 蒙克:云计算安全问题被夸大
 - 下一篇: 编程语言分类及python所属类型