mogodbshell中數組物件查詢修改方法

來源:互聯網
上載者:User

標籤:記錄   style   cti   obj   roman   需求   標題   查詢   pre   

在mongodb中,存在如下資料

{ "_id" : ObjectId("59af55078a8fc5e51ff425de"), "title" : "title1", "col" : "col1", "reader" : [ { "readername" : "jim", "isread" : true }, { "readername" : "kate" }, { "readername" : "lilei" } ], "begindate" : "Wed Sep 06 2017 09:53:11 GMT+0800 (中國標準時間)" }{ "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readername" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09:53:50 GMT+0800 (中國標準時間)" }{ "_id" : ObjectId("59af55458a8fc5e51ff425e0"), "title" : "title3", "col" : "col1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" } ], "begindate" : "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" : "col1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readername" : "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" : "col1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readername" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "Wed Sep 06 2017 09:53:50 GMT+0800 (中國標準時間)" }

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

83334129

mogodbshell中數組物件查詢修改方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.