Learn MongoDB Five: MongoDB query (array, inline document) (ii)

Source: Internet
Author: User
Tags mongodb query


First, Introduction Our last article introduced Db.collection.find () can be implemented based on conditional queries and specifying fields returned using the projection operator omit this parameter to return all fields in the matching document, we present today the query operations for array and inline documents, especially for the $ Elemmatch can also use the second parameter of the Find method to limit the elements in the returned array, returning only the description of the document we need. instead of returning an array of inline documents, we often need to return the main document and return only the values we need in the inline document array when querying the query condition for an inline document array.
Second, the log group according to the conditions of query

$all, $size, $slice,$elemMatch

(1) $all find the document that contains the specified value in the array

Grammar:

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

Example:

Db.orders.find ({"Books": {$all: ["Java", "Mongo"]}})

Find books contains Java, MONGO document data

(2) $size find a document with an array size equal to a specified value

Grammar:

<span style= "FONT-SIZE:18PX;" >    {field: {$size: number}}</span>


Example:

<span style= "FONT-SIZE:18PX;" >    db.orders.find ({"Books": {$size: 2}}) </span>

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

Grammar:

<span style= "FONT-SIZE:18PX;" >         db.collect.find ({},{field:{$slice: number}}) </span>

Number Description:

A positive value returns the number of values previously specified: for example, 1 returns the first array of

A negative number returns the count of the values specified for the reciprocal: 1 returns the first of the array, for example

Example:

<span style= "FONT-SIZE:18PX;" >       db.orders.find ({"Onumber": {$in: ["008", "009"]}},{books:{$slice: 1}}) </span>

1) $slice can query the array from the first to the first few

Grammar:

<span style= "FONT-SIZE:18PX;" >        db.collect.find ({},{field:{$slice: [Number1, Number2]}}) </span>


Skips the number1 position of the group and returns the number of Number2

Number1 Description:

Indicates the number of arrays to jump to a specified value: for example, 2 jumps to the 3rd array

A negative number means the inverse of the array to jump to the specified value: for example, 2 jumps to the 3rd of the array.

Example:

<span style= "FONT-SIZE:18PX;" >        db.orders.find ({"Onumber": {$in: ["008", "009"]}},{books:{$slice: [/]}) </span>



Skips the first element of a books array, now to the second element of the array, and returns 1 elements

three, an array of embedded document query

Let's 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 document contains an element that is an array, $elemmatch can match elements within the array and return the document data

Grammar:

<span style= "FONT-SIZE:18PX;" >       {field:{$elemMatch: {field1:value1, Field2:value2,.........}}} </span>

Example:

<span style= "FONT-SIZE:18PX;" >    db.orders.find ({"items": {$elemMatch: {"Quantity": 2}}) </span>

Returns a document with a quantity of 2

You can also query Db.orders.find ({"Items.quantity": 2})

(2) $elemMatch can carry multiple query conditions

Example:

<span style= "FONT-SIZE:18PX;" >   db.orders.find ({"items": {$elemMatch: {"Quantity": 4, "ino": "002"}}) </span>

The quantity in our query array equals 4 and Ino equals 002, but we want to return this document with quantity equal to 4 in the array and Ino equal to 002, and do not want to return these unrelated documents such as Ino equals 001.

(3) $elemMatch can also use the second parameter of the Find method to limit the elements in the returned array, returning only the documents we need

Example:

Db.orders.find ({"Onumber": "001"},{"items": {$elemMatch: {"Quantity": 4, "ino": "002"}}, "CNAME": 1, "date": 1, " Onumber ": 1})

We only return quantity equals 4 and Ino equals 002 of the document, unrelated documents are not returned, so that we can process the data, so as to save the amount of data transmission, reduce memory consumption, improve performance, when the data is large, performance is obvious.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Learn MongoDB Five: MongoDB query (array, inline document) (ii)

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.