MongoDB Various inquiries operation detailed _MONGODB

Source: Internet
Author: User
Tags mongodb

One, find operation

Find is used in MongoDB 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 key value that only returns interest when the query is queried, and is implemented by specifying the second parameter of find. This saves the amount of data transferred and saves the time and memory consumption of the client decoding the document.


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

Second, the inquiry condition

1, compare the query

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


2. Related Query

$in to query for multiple values of a key, $nin returns a document that does not match all of the criteria in the filtered array. Combining $in with $not can achieve the same effect as $nin.


$or is used to make or query on multiple keys.


Iii. Specific types of queries

1, NULL query

Null not only matches itself, it can also match documents that do not exist in the key.

2, Regular expression


3. Array query

$all: Matches an array with multiple elements.


$size: Queries the array of the specified length.


$slice: Returns a child collection of the array.


4. Embedded document Query


Four, $where inquiries

$where query is an advanced query section of MongoDB that can perform arbitrary JavaScript as part of a query and is a complement to other queries.


$where queries need to convert each document from Bson to a JavaScript object, and then run through an $where expression that cannot take advantage of the index, so the query speed is much slower than the regular query. If you must use, you can use the general query as a front filter, you can take advantage of the index using the index based on the $WHERE clause filtering, and finally using $where to tune the results. Another way is to use the mapping of Jane-mapreduce.

V. Cursors

Cursors are useful things that the MongoDB database uses Grand to return find execution results. The client uses cursors to effectively control the end result, such as paging, sorting.


1, Limit,skip and sort

Limit: Limit the number of results, the limit is the limit.
Skip: Skip the top n documents that match to, and then return the remaining documents. Skipping over too many documents results in performance problems and is recommended as much as possible.
Sort: The document is sorted according to the specified key, 1 is ascending, and-1 is descending.


2. Advanced query Options

Packing 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 start condition of the query.
$max:d ocument, the end condition of the query.
$hint:d ocument, specify which index the server uses to query.
$explain: Boolean, get the details of the execution of the query (the index used, the number of results, the time elapsed, 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 the query executes to avoid inconsistent reads.

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

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

3. The inside of the cursor
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 completes the matching result the iteration time automatically clears.

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

3, timeout to destroy, you can use the immortal function to turn off the cursor timeout, this operation must be done after the result of the iteration to close the cursor.

Vi. Summary

Using MONGODB requires a reasonable design of the document structure to meet specific requirements. For example, randomly select the document, skip the random document with skip to add a random key in the document, and then use a random number to query the document efficiently, random keys can also add indexes, more efficient. Reasonable choice and reasonable design.

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.