MongoDB Query Skills Summary _mongodb

Source: Internet
Author: User
Tags findone mongodb mongodb query

In MongoDB, the Db.collection.find () method is used to retrieve documents from the collection. The Db.collection.find () method returns a cursor that retrieves the document. The Db.collection.findOne () method also performs a read operation, returning a document. On the internal implementation, the Db.collection.findOne () method is Db.collection.find () using limit 1.

All documents in the query collection:

1. An empty query document ({}) can detect all documents in a collection:

Copy Code code as follows:
Db.inventory.find ({})

2. Find () without specifying the query document is equivalent to a query that specifies an empty query document. Therefore, the following query is equivalent to the query above:

Copy Code code as follows:
Db.inventory.find ()

Specify equality conditions:
Use the {<field>:<value>} document to specify an equality condition to query all documents that contain <field> fields with a value of <value>. The following example retrieves all documents of the Type field value of snacks from the inventory collection:

Copy Code code as follows:
Db.inventory.find ({type: "Snacks"})

Use the query operator to specify criteria:
You can use the query operator to specify criteria in MongoDB. The following example queries the value of the Type field from the inventory collection as ' food ' or ' snacks ':

Copy Code code as follows:
Db.inventory.find ({type: {$in: [' food ', ' Snacks ']}})

Although this query can use the $or operator, $in is used instead of $or for equality detection for the same field.

Specify AN AND condition:
A conforming query can specify multiple document fields in a condition. A composite query condition, together with a logical and join, that queries for documents that meet all criteria. In the following example, the query document specifies criteria equal to the food and price fields that are less than ($LT) specified values:

Copy Code code as follows:
Db.inventory.find ({type: ' food ', Price: {$lt: 9.95}})

This query selects documents that all Type field values are equal to food and that the Price field value is less than 9.95.

Specify an OR condition:
Using the $or operator, you can specify a composite query that uses a logical or connection, and the query selects a document that matches at least one condition in the collection. In the following example, all Qty field values in the query collection are greater than ($GT) 100, or documents with a value less than ($LT) 9.95 in the Price field:

Copy Code code as follows:
Db.inventory.find (
{$or: [
{Qty: {$gt: 100}},
{Price: {$lt: 9.95}}
]
}
)

Specify both and and or conditions:
With more criteria, you can specify an exact query condition. In the following example, all of the Type field values in the query document selection collection are ' food ' and the Qty value is greater than ($GT) 100 or the price value is less than ($LT) 9.95:

Copy Code code as follows:
Db.inventory.find ({type: ' food ', $or: [{qty: {$gt: 100}},
{price:{$lt: 9.95}}
]
} )


Sub-document:
When a field contains an embedded document (that is, a subdocument), you can specify the value of the entire subdocument as a field, or the value of each field in the subdocument by using the dot symbol "go" to the child file:
1. Exact matching subdocuments:
Specifies an equality condition that matches the entire subdocument and uses the query document {<field>:<value>},<value> to match the subdocument. The "equality" matching subdocument requires that the fields of the subdocument exactly match <value> conditions, including the order of the fields. The following example queries the value of the producer field to match a company field with a value of "ABC123" and a subdocument with a value of "123 Street", in order:
Copy Code code as follows:
Db.inventory.find (
{
Producer: {
Company: ' ABC123 ',
Address: ' 123 street '}
}
)


2. Sub-document Field "equality" match:
The field in the query collection for the subdocument of the specified field contains the document for the specified condition. The following example uses the dot notation query for all documents with the Conmpany field value "ABC123" for the producer subdocument:
Copy Code code as follows:
Db.inventory.find ({' Producer.company ': ' ABC123 '})

Array:
When a field value is an array, you can use an array to match exactly or specify a value in the array. If the array element is a subdocument, you can use the dot notation to specify the field.
Exact Match array:
Specify an equality condition in the array, using the query document {<field>:<value>},<value> is the array to use for the match. An exact match of an array requires that the fields of the array exactly match the specified <value>, including the order of the elements:

Copy Code code as follows:
Db.inventory.find ({tags: [' fruit ', ' food ', ' Citrus ']})

match an array element:
You can specify an individual element in the array for an "equality" match. This specification matches an array that requires at least one specified element to be included. The following example will have all tags is an array and contains "fruit" elements of the document:
Copy Code code as follows:
Db.inventory.find ({tags: ' fruit '})

matches the specified element of the array:
Matches the specified index or location in an array to a document with equal conditions. In the following example, use the dot notation to match all tags is an array, and the first element is a document of "fruit":
Copy Code code as follows:
Db.inventory.find ({' tags.0 ': ' Fruit '})

Sub-document array:

To match a subdocument's field using an array index:
If you know the index of an array of subdocuments, you can specify the location of the subdocument. The following example queries all memo containing an array and the first element is a subdocument, and the subdocument's by field value is "Shipping":

Copy Code code as follows:
Db.inventory.find ({' memos.0.by ': ' Shipping '})

matching a field does not use an array index:
If you do not know the index position of the subdocument, use the field name and the subdocument field name of the dot-attached array. The following example queries that all memos fields are an array, and that the array contains at least one document with a subdocument with a by field value of "Shipping":
Copy Code code as follows:
Db.inventory.find ({' memos.by ': ' Shipping '})

match multiple fields:
To match more than one field in a subdocument, you can use the dot symbol or the $elemmatch operator. The following example uses the dot notation query memos The field value is an array, and the subdocument Memo field equals "on Tiem" and the By field is equal to the "Shipping" document:
Copy Code code as follows:
Db.inventory.find (
{' Memos.memo ': ' On Time ', ' memos.by ': ' Shipping '}
)

The following example uses the $elemmatch operator to query that the Memos field value is an array, and that the subdocument Memo field equals "on Tiem" and the By field equals the document for "shipping":
Copy Code code as follows:
Db.inventory.find ({
Memos: {
$elemMatch: {
Memo: ' On Time ',
By: ' Shipping '}
}
}
)

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.