How MongoDB queries and modifies inline documents

Source: Internet
Author: User
Tags modifier

MongoDB is a document-based database system, DOC is the data unit of MongoDB, each doc corresponds to a relational database row (row), the difference between Doc and row is the atomicity of field: column in row is not and split atomic object, The field in Doc can be an atomic object, or it can be a data type such as an inline doc (embedded doc), an array, and so on. All field keys in the embedded doc are not allowed to be duplicated.

For example the following doc,contact field is an inline doc.

onedoc={name:"T1", Age:+,contact:    {        phone:123,        email: "[Email protected]}    }

--insert a doc
Db.foo.insert (Onedoc)

One, query the embedded document
1, if the inline doc exists field satisfies the query filter, then the doc is returned. When referencing field in the inline doc, use dot notation, in the format: Embedded_doc.field:value, if the embedded Doc field is also an inline document, and so on, embedded_doc1.embedded_ Doc2.field:value.

example, query the contact field where phone is 123 of all Doc.

Db.foo.find ({"Contact.phone": 123})

2, limit the returned Doc's field
example, using the second parameter of find, projection doc, returns only two field:name fields and an inline doc email field. by dot notation, some fields in the inline doc are returned in the format: Embedded_doc.field:1, which indicates that the field is returned, embedded_doc.field:0, indicating that the field is not returned.

Db.foo.find ({"Contact.phone": 123},{_id:0, "Contact.email": 1,name:1})

Second, modify the inline doc

The Upsert option is useful if an inline document does not exist in the current doc, is incremented by the $set modifier, and if an inline document exists in the current doc, the value of the inline document is modified by $set modifier.

1, modify doc, add inline doc

example, add the Address field, which is an inline doc

Db.foo.updateMany ({name:"T1"},{$set: {address:{province:"Henan", City: "Xinyang"}}},{upsert :true})

2. Modify the fields in the inline doc
example, modify the contents of the province and city fields in the inline doc, all modified to "Shanghai"

Db.foo.updateMany ({name:"T1"},{$set: {address:{province:"Shanghai", City: "Shanghai"}}},{ Upsert:true})

Third, remove the inline doc in doc
$unset modifier be able to delete a field in doc using the format: {$unset: {field1: "", Field2: "}}, the deleted field is placed in the $unset document.

1, if you want to delete the field in the inline doc or the elements in the array, you can use dot notation.

example, remove the province field in the address inline doc

Db.foo.updateMany ({name:"T1"},{$unset: {"address.province": ""}},{upsert:True })

2, if you do not use dot notation, then the entire embedded doc will be deleted

example, in $unset modifier, the Address field is removed by using the address inline doc format.

Db.foo.updateMany ({name:"T1"},{$unset: {address:{province:"Shanghai"}}},{upsert:  True})

Referencing Doc:

$set

$unset

How MongoDB queries and modifies inline documents

Related Article

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.