MongoDB various query operation detailed

Source: Internet
Author: User

Copy Link: http://www.jb51.net/article/48216.htm

One: Find operation:

MongoDB uses find to query, and all and part of the query can be implemented by specifying the first parameter of find.

1. All inquiries

The empty query document {} will match the entire contents of the collection. If you do not specify a query document, the default is {}.

2, part of the query

3, the key filter

A key filter is a query that returns only the key values that interest you, and is implemented by specifying the second parameter of find. This saves the amount of data transferred and saves the time and memory consumed by the client decoding the document.

When querying, the value of the query document that the database cares about must be constant.

Ii. Conditions of Inquiry

1. Compare queries

$lt, $lte, $gt, $gte, $ne and <,<=,>,>=,!= are one by one, and they can be combined to find values within a range.

2. Related queries

$in multiple values for querying a key, $nin will return documents that do not match all the conditions in the filter array. Combining $in and $not can achieve the same effect $nin.

$or used to make or queries on multiple keys.

Iii. Specific types of queries

1. Null query

Null not only matches itself, but also matches a document where the key does not exist.

2. Regular expressions

3. Array query

$all: Matches an array by multiple elements.

$size: Queries the array of the specified length.

$slice: Returns a sub-collection of arrays.

4. Embedded document Query

Iv. $where Enquiry

$where query is the advanced query part of MONGODB, which can execute arbitrary JavaScript as part of the query and is a complement to other query methods.

$where queries need to convert each document from Bson to a JavaScript object and then run through the $where expression, which cannot take advantage of the index, so queries are much slower than regular queries. If you have to use a regular query as a pre-filter, you can use the index to filter by non-$where clauses, and finally use $where to tune the results. Another way is to use the mapping simplification-mapreduce.

V. Cursors

Cursors are useful things, and the MongoDB database uses cursors to return the execution results of find. The client uses cursors to effectively control the final results, such as paging and sorting.

1. Limit,skip and sort

Limit: Limits the number of results, the limit is the upper limit.
Skip: Skip to the first n documents that match, and then return to the remaining documents. Skip skipping too many documents will cause performance problems, and it is recommended to avoid them as much as possible.
Sort: Sorts the document by the specified key, 1 is ascending, and 1 is descending.

MongoDB various query operation detailed

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.