Learning MongoDB 5: MongoDB query (array, embedded documentation) (2), mongodb embedded

Source: Internet
Author: User
Tags mongodb query

Learning MongoDB 5: MongoDB query (array, embedded documentation) (2), mongodb embedded

1. Introduction We introduced db in the previous article. collection. find () can be used to query conditions and specify fields returned by using the projection operator to omit this parameter to return all fields in the matching document. Today we have introduced query operations for arrays and embedded documents, especially$ ElemMatch can also be used in the second parameter of the find method to limit the elements in the returned array. Only the introduction of the document we need is returned.When we query the nested document array by using the query conditions, we only need to return the main document and the values we only need in the embedded document array, instead of returning the arrays of embedded documents.
2. query arrays based on conditions

 

 $ All, $ size, $ slice, $ elemMatch

(1) $ all search for documents with specified values in the array

Syntax:

{Field :{$ all: [<value >,< value1>...]}

Example:

Db. orders. find ({"books": {$ all: ["java", "mongo"]})

Search for documents whose books contain java and mongo

 

(2) $ size: search for documents whose array size is equal to the specified value

Syntax:

   {field: {$size: number } }


Example:

          >db.orders.find({"books":{$size:2}})

 

(3) $ slice query the number of returned elements specified in the array

Syntax:

         >db.collect.find({},{field:{$slice: number }})

 

Number Description:

If the number is positive, the number of the previously specified values is returned. For example, 1 returns the first value of the array.

If it is a negative number, the number of values specified to the last is returned. For example,-1 returns the first to the last value of the array.

 

Example:

        >db.orders.find({"onumber":{$in:["008","009"]}},{books:{$slice:1}})

 

 

 1) $ slice can query the number to number in the array

Syntax:

            >db.collect.find({},{field:{$slice:[ number1, number2] }})


Skip the number1 position of the array and return the number of number2.

Number1 description:

A positive number indicates the number of arrays that are jumped to the specified value. For example, 2 indicates the number of arrays that are jumped to the specified value.

If it is a negative number, it indicates that the number of records to which an array belongs is exceeded. For example,-2 indicates that the number of records to which an array belongs is exceeded.

 

Example:

           >db.orders.find({"onumber":{$in:["008","009"]}},{books:{$slice:[1,1]}})



Skip the first element of the books array. Now, the second element of the array is skipped, and 1 element is returned.

 

 

3. query embedded array documents

We save the data first.

<span style="font-size:18px;"> db. orders.insert([{        "onumber" : "001",         "date" : "2015-07-02",         "cname" : "zcy1",          "items" :[ {                   "ino" : "001",                  "quantity" :2,                   "price" : 4.0                 },{                   "ino" : "002",                  "quantity" : 4,                   "price" : 6.0                }                ]},{         "onumber" : "002",         "date" : "2015-07-02",         "cname" : "zcy2",          "items" :[ {                  "ino" : "001",                  "quantity" :2,                   "price" : 4.0                   },{                  "ino" : "002",                  "quantity" :6,                   "price" : 6.0                 }               ]}])</span>


(1) $ elemMatch contains an array, so $ elemMatch can match the elements in the array and return document data.

Syntax:

         >{field:{$elemMatch:{ field1:value1, field2:value2,………}}}

 

Example:

         >db.orders.find({"items":{$elemMatch:{"quantity":2}}})

Returns the document whose quantity is 2.

 

You can also query db. orders. find ({"items. quantity": 2 })

 

(2) $ elemMatch can contain multiple query Conditions

Example:

      >db.orders.find({"items":{$elemMatch:{"quantity":4,"ino":"002"}}})

 

We query the document in which quantity is 4 and ino is 002, but we want to return the document in which quantity is 4 and ino is 002, we do not want to return irrelevant documents such as ino and 001.

 

 

(3) $ elemMatch can also be used in the second parameter of the find method to limit the elements in the returned array. Only the required documents are returned.

Example:

Db. orders. find ({"onumber": "001" },{ "items" :{$ elemMatch: {"quantity": 4, "ino": "002 "}}, "cname": 1, "date": 1, "onumber": 1 })

We only return documents whose quantity is 4 and whose ino is 002. unrelated documents are not returned, which facilitates data processing. This reduces the amount of data transmitted, reduces memory consumption, and improves performance, when the data is large, the performance is obvious.




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.