MongoDB nested queries and modifications

Source: Internet
Author: User
Tags mongodb

MongoDB Introduction to nested queries in each document don't know where to hide, anyway I didn't find, a chance to find a post on the web, finally know the use of nested queries. So one of the problems in our application has been solved. Don't talk nonsense, now say the use of nested queries.

Suppose there is a collections in the MongoDB, and the data is as follows:

> Db.xx.find ()
{"_id": 1, "name": {"a": 2, "Last": 3}}
{"_id": 3, "name": {"1": 1, "2": 2, "3": 3}}
{"_id": 2, "name": {"1": 1, "2": 2}}

Now we're going to query the value of the _id=1 name in the record, and do the following:

> Db.xx.find ({"_id": 1},{"Name.first": 1, "_id": 0})
{' name ': {' 2}} '

OK, we know from the above example that the key to nested queries is "Name.first". Modifications to nested properties are similar, for example, if we want to name.first the query to the value of +2 in this record, then:

> db.xx.update ({"_id": 1},{$inc: {"Name.first": 2}})
> Db.xx.find ({"_id": 1},{"Name.first": 1, "_id": 0})
{' name ': {' 4}} '

OK, the value has changed.

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.