Mongodb entry-6 query 1

Source: Internet
Author: User
Mongodb entry-6 query 1 I feel that the most used database query is used, so after learning the query, we will be very comfortable in future development and it is very easy. Mongodb also provides powerful query functions. I will try my best to explain these problems in detail here. If there are any errors, please point them out. Find all documents in a collection in mongodb

Mongodb entry-6 query 1 I feel that the most used database query is used, so after learning the query, we will be very comfortable in future development and it is very easy. Mongodb also provides powerful query functions. I will try my best to explain these problems in detail here. If there are any errors, please point them out. Find all documents in a collection in mongodb

Mongodb entry-6 query 1

I feel that the most used database query is used, so after learning the query, we will be very comfortable in future development and it is very easy. Mongodb also provides powerful query functions. I will try my best to explain these problems in detail here. If there are any errors, please point them out.

Search for all documents in a collection

Use the find method to query in mongodb. The method is db. set Name. find ({}). The parameter of the find method is a json object, which is more specifically a bson object. But if someone says a json object, you can understand it. When we use the find method, if no parameter is passed, it is an empty json passed, that is, to query all documents in the set. As follows:

[Html]

> Db. user. find ()

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b8"), "name": "user6", "age": 6}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}

{"_ Id": ObjectId ("5198c286c686eb50e2c843ba"), "name": "user8", "age": 8}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bb"), "name": "user9", "age": 9}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}

Conditional Query

Since find has a parameter, we can pass the parameter to it. We query the document with name user0 as follows:

[Html]

> Db. user. find ({name: "user0 "})

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}

Returns some document values.

Here we found the document named user0, but sometimes we only want to get some values of this document. At this time, we can pass the second parameter to find, the parameters here are json in the same way as the first one. We only want to get the value of age, which can be written as follows:

[Html]

> Db. user. find ({name: "user0" },{ age: 1 })

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "age": 0}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "age": 20}

Do not show certain values in the document

In the above Code, we set age to 1, which means we need to get the age value. If we don't want the age value, we can set it to 0. Similarly, true, false, and 1 and 0 serve the same purpose here. Another point where the number used in mongodb is not 0 is true, but other numbers are not recommended, we can remove the age value using the following method:

[Html]

> Db. user. find ({name: "user0" },{ age: 0 })

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0 "}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0 "}

Conditional expressions

In mongodb, you can use the conditional expression <,>, <=,> =, where no = is used, because the query above is equal to =, the method corresponding to the character above in mongodb is $. lt, $. gt, $ lte, $ gte.

We query users older than 5 as follows:

[Html]

> Db. user. find ({age: {$ gt: 5 }})

{"_ Id": ObjectId ("5198c286c686eb50e2c843b8"), "name": "user6", "age": 6}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}

{"_ Id": ObjectId ("5198c286c686eb50e2c843ba"), "name": "user8", "age": 8}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bb"), "name": "user9", "age": 9}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}

Search for users of age greater than or equal to 5, as shown in the following code:

[Html]

> Db. user. find ({age: {$ gte: 5 }})

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b8"), "name": "user6", "age": 6}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}

{"_ Id": ObjectId ("5198c286c686eb50e2c843ba"), "name": "user8", "age": 8}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bb"), "name": "user9", "age": 9}

{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}

Similarly, $ gt/$. gte is used in the same way as above.

Limit limits the number of returned documents

Here we will introduce the limit method, which is to limit the number of returned documents. Use:

[Html]

> Db. user. find ({age: {$ gte: 5}). limit (3)

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b8"), "name": "user6", "age": 6}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}

Compared with the above, only three data items are displayed.

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.