Mongodb Note 03

Source: Internet
Author: User

Inquire

1. MongoDB uses find to query. The first parameter of find determines which documents to return, and this parameter is a document that specifies the query criteria. An empty query matches the entire contents of the collection. If you do not specify a query, the default is {}.

2. You can specify the desired key by using the second parameter of find. This saves the amount of transmission and saves the time and memory consumption of the client decoding the document. Example: Db.users.find ({},{"username": 1, "email": 1})

3. By default, the "_id" key is always returned, even if you do not specify that you want to return this key. Can be rejected by the second parameter.

4. A second parameter can be used to reject a key-value pair in the query structure. Example: Db.users.find ({},{"_id": 0}) so that "_id" is not returned.

5. Query criteria

1). Query criteria: "$lt", "$lte", "$gt" and "$gte" are all comparison operators, respectively, corresponding to <, <=, > and >=. Example: Db.users.find ({"Age": {"$gte": "$lte": 30}}). "$ne" means not equal.

2). or query:

A. "$in" can be used to query multiple values for a key: Db.raffle.find ({"Ticket_no": {"$in": [725,542,390]}})

B. "$nin" will return content that does not match all the conditions in the array: Db.raffle.find ({"Ticket_no": {"$nin": [725,542,390]}})

C. "$or" can find any given value in multiple keys: Db.raffle.find ({"$or": [{"Ticket_no": 725},{"Wenner": True}]})

3). $not "

A. The "$mod" modulo operator, "$mod" divides the queried value by the first given value, and succeeds if the remainder equals the second given value: Db.users.find ({"Id_num": {"$mod": [5,1]}})

B. If the query has the opposite result: Db.users.find ({"Id_num": {"#not": {"$mod": [5,1]}}}).

C. "$not" is extremely useful when used in conjunction with regular expressions to find documents that do not match the specific pattern of the fish.

4). Conditional semantics: The conditional statement is the key of the inner document, and the modifier is the key of the outer document.

6. When using normal and type queries, you always want to limit the range of results to the minimum possible conditions. The or type query is the opposite: the first query criteria should match as many documents as possible, which is most efficient. "$or" in any case will

Normal work. If the query optimizer can handle "$in" more efficiently, then choose to use it.

7. Specific query conditions:

1). Null:db.c.find ({"Y": null}). Null not only matches a document with a key value of NULL, but also matches a document that does not contain the key.

If you only want to match a document with a key value of NULL, the Minutes check if the value of the key is null and the key value already exists through the $exists condition. Db.c.find ({"Z": {"$in": [null], "$exists": True}})

Mongodb Note 03

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.