日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mogodbshell中数组对象查询修改方法

發布時間:2024/4/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mogodbshell中数组对象查询修改方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在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 (中國標準時間)" }

核心是$,可以理解為數組定位器

需求3:刪除名為title2下的reader名字為lilei的讀者對象:

?

//未刪除前數據 { "_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 (中國標準時間)" }//執行命令 > db.articles.update({title:'title2','reader.readername':'lilei'},{$pull:{'reader':{readername:'lilei'}}})//執行命令后查詢的結果 > 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命令查找數組名稱,然后通過屬性值刪除數組內的對象記錄

?

轉載于:https://www.cnblogs.com/qkabcd/p/7483357.html

總結

以上是生活随笔為你收集整理的mogodbshell中数组对象查询修改方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。