MongoDB (v): MongoDB additions and deletions (3)----query details (UP)

Source: Internet
Author: User

MONGOBD's query is plainly a find () method. But it's still a little complicated. Come on

(1) Query all data: DB. Collection name. Find (); There's nothing to say.

(2) Query the first data: DB. Set name. FindOne (); There's nothing to say.

(3) Condition query: DB. Set name. Find (query condition), query condition is also a kind of JSON format. So understanding JSON is really important to learn about MongoDB.

As follows: query for records with name Zhangsan:

(4) Combination condition query: db.collection name. FIND ({query condition 1, query Condition 2, ...}), or the above example, now assume that you want to query name Zhangsan,

Age is 23:db.customer.find ({"Name": "Zhangsan", "Age": 23})

(5) Returns the specified field: for example, in a relational database select * ... is to return all fields, select field 1, Field 2 .... is to return two fields, then the Mong

ODB How do I return to the specified field? Format: db.collection name. FIND ({query condition 1, query criteria 2,...},{field 1:1, field 2:1,....}),

1 of them represent display, if 0 is not displayed. The default is all displayed. If you do not want to display _id, you must display the display of _id as 0, as follows:

(6) Scope query: First look at the comparison operator. $lt (<), $gt (>), $lte (<=), $gte (>=), $ne (! =). Examples are as follows:

(7) $in query: The query value is matched within the specified collection. is similar to in SQL. As follows:

Query the information for a user named Zhangsan or Lisi:

(8) $nin query: Matches records that are not in the specified collection condition. is similar to not in SQL, for example, to query for information that name is not Zhangsan or Lisi:

Db.customer.find ({"name": {"$nin": ["Zhangsan", "Lisi"]}})

(9) $or query: Yes or no, on the example: Query the information for a user with name Zhangsan or age 13:

Db.customer.find ({"$or": [{"Name": "Zhangsan"}, {"Age": 13}]}), as shown below:

Write a slightly more complex query _id in 001 or 002, or the user with name Zhaoliu:

Db.customer.find ({"$or": [{"_id": {"$in": ["001", "002"]}}, {"Name": "Zhaoliu"}]}). As shown below:

(Ten) $not query: The difference between $not and $nin is that $nin is only available on the collection. The $not range is wide.

Query _id information for users who are not in 001 and 002: Db.customer.find ({"_id": {"$not": {"$in": ["001", "002"]}})

Null: Now collection has the following document:

You can see that some document has sex, some are not, some are null, enter the following command: Db.customer.find ({"Sex": null}), the result is as follows:

From the results can be seen, not only sex is null query out, and no sex field document also queried out. If you want to find an age of NULL, you should also

the base Age must be limited. What then? Let's put it down and say the next one.

$exists: Determines whether a field exists. For example, query all records that exist in the Name field: Db.customer.find ({"name": {"$exists": true}), check

Consulting Institute There are no records for the sex field: Db.customer.find ({"Sex": {"$exists": false}}). Now solve the problem in (11):

But one thing feels strange: Why not write like this?

In fact, think carefully also understand: because the wording of the condition is to follow the JSON format, in the JSON, the key can not be repeated, when repeated, the latter will cover the former, I think probably

just That 's the reason.

(12) Regular expression: direct on Example: Query the information for the user with the Ang in name:db.customer.find ({"Name":/ang/})

Query for user information that starts with Z:db.customer.find ({"Name":/^z/})

Query information for users ending with an :db.customer.find ({"Name":/an$/})

MongoDB (v): MongoDB additions and deletions (3)----query details (UP)

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.