Array object Query modification method in Mogodbshell

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.