MongoDB cursors, Limit,skip,sort operations

Source: Internet
Author: User
Tags mongodb regular expression sort

Using cursors, you can control the results of a query:

Cursors implement an iterator interface that can be used in foreach ():

When a query is assigned a variable, find () does not immediately query the database. For example: Var cursor=db.test.find (). Queries are sent to the database only when Cursor.hasnext () is executed. Therefore, some query filtering conditions are added before the query.

Limit () function: it specifies the upper limit. Find () By default, returns only the previous 20 results, by using the limit () function, you can limit the returned results, ignore a certain number of results, and sort them. These operations must be performed before they are sent to the database.

Skip () function: Skips the specified number of documents, returns the remaining documents, and does not return any documents if the number of files in the collection is less than the specified number.

Sort () function: Takes a set of key-value pairs as parameters, where the key is the key value in the document, and the value represents the direction of the sort. Where 1 means ascending, 1 indicates descending order.
If more than one key is specified, the keys are sorted in the order specified:

Key-Value comparison priority (from small to Large):
Minimum –>null–> number (integer/Long integer/Double) –> string –> Object/document –> array –> binary Data –> object id–> boolean –> datetime –> timestamp –> Regular Expression- > Maximum Value

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.