MongoDB various query operation detailed

Source: Internet
Author: User

This article mainly introduces MongoDB various query operation details, including comparison query, correlation query, array query and so on, the need for friends can refer to the next

One, find operations

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.

2. Advanced query Options

Package query: Use Sort,limit or skip to further control the final result.

Useful configuration options:

$maxscan: Integer that specifies the maximum number of documents scanned by the query.
$min:d ocument, the starting condition for the query.
$max:d ocument, the end condition of the query.
$hint:d ocument to specify which index the server uses to query.
$explain: Boolean, which gets the details of the query execution (the index used, the number of results, time-consuming, and so on), rather than actually executing the query.
$snapshot: Boolean to ensure that the result of the query is a consistent snapshot at the moment of query execution to avoid inconsistent reads.

The wrapper query wraps the query criteria into a larger query document, such as when you execute the following query:

Db.foo.find ({"Name": "Bar"}). Sort ("x": 1)
The shell converts the query from {"name": "Bar"} to {"$query": {"name": "Bar"}, "$orderby": {"X": 1}, rather than sending {"name": "Bar"} as a query document to the database.

3. Cursor Insider
Look at the two angles of the cursor: the client's cursor and the database cursor represented by the client cursor (server side).

On the server side, cursors consume memory and other resources, so they need to be released as soon as reasonably possible. The server side causes the cursor to terminate as follows:

1. The cursor is automatically cleared when the iteration of the matching result is completed.

2, the cursor in the case of the client is no longer in scope, the driver will send a special message to the server, let it destroy the cursor.

3, time-out destruction, you can use the immortal function to close the cursor time-out, take this action must be completed after the result of the iteration of the cursor closed.

Vi. Summary

Using MONGODB requires a reasonable design of the document structure to meet certain specific requirements. For example, randomly selecting a document, skipping a random document with Skip and not adding a random key to the document, and then using a random number to query the document efficiently, the random key can also be indexed, more efficient. Reasonable choice, reasonable design.

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.