MongoDB common Operations--collection 2

Source: Internet
Author: User

1. Query the document in the collection, you can use the command DB. Collection name. Find ({condition}), or use DB. Collection name. FindOne () query the first document

2. Query the document in the collection to return some specific key values



3. Query the document in the collection, using the conditional expression (<, <=,;, >=,!=)

Greater than: field > Value
Db.collection.find ({field:{$gt: value}});

Less than: Field < value
Db.collection.find ({field:{$lt: value}});

Greater than or equal to: field >= value
Db.collection.find ({field:{$gte: value}});

Less than equals: field <= value
Db.collection.find ({field:{$lte: value}});

Not equal to: field! = value
Db.collection.find ({field:{$ne: value}});

4. Querying the collection for documents, statistics (count), sorting (sort), paging (skip, limit)

Db.customer.count ();
Db.customer.find (). Count ();
Db.customer.find ({age:{$lt: 5}}). Count ();
Db.customer.find (). Sort ({age:1});
Db.customer.find (). Skip (2). Limit (3);
Db.customer.find (). Sort ({age:-1}). Skip (2). Limit (3);
Db.customer.find (). Sort ({age:-1}). Skip (2). Limit (3). Count ();
Db.customer.find (). Sort ({age:-1}). Skip (2). Limit (3). Count (0);
Db.customer.find (). Sort ({age:-1}). Skip (2). Limit (3). Count (1);

5. Querying a document in a collection, $all is primarily used to query the containing relationship in an array, and the query condition does not return as long as there is one that does not contain

6. Query the document in the collection, $in, similar to in in the relational database

7. Query the document in the collection, $nin, as opposed to $in

8. Query the document in the collection, $or, equivalent to or in a relational database, or a relationship, such as querying a document with name User2 or age 3,
The command is: Db.customer.find ({$or: [{name: "User2"},{age:3}]})

9. Query the document in the collection, $nor, to filter out some of the data according to the criteria, for example, the query name is not a user2,age 3 document,
The command is: Db.customer.find ({$nor: [{name: "User2"},{age:3}]})

10. Query the document in the collection, $exists, to query a document that has a key in the collection, or a document that does not have a key, such as querying all documents that have the name key in the customer collection, you can use Db.customer.find ({name:{$exists : 1}}),
$exists: 1 means true, meaning existence
$exists: 0 means false, meaning that there is no

11. Query the collection of documents, similar to relational database, MongoDB also has the concept of cursors

MongoDB common Operations--collection 2

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.