MongoDB authoritative Guide to learn notes 4---query related knowledge points

Source: Internet
Author: User
Tags findone

1 Find

Find ({query condition},{"key": 1, "email": 1}) after which keys are returned

2 Available comparison operators

$lt, $lte, $GT, $gte

such as Db.users.find ({"Age": {"$gte": +, "$lte": 30}})

3 Not equal to

Find (..... {"Key": {"$ne": "Value"}}

4 in

Find (..... {"Key": {"$in": [[i]}}

5 nin

Find (..... {"Key": {"$nin": [[i]}}

6 or

Find ({"$or": [{"Key1": "Value1"}, {"Key2": "Value2"}]})

Find ({"$or": [{"Key1": {"$in": ["value1", "Value2"]}}, {"Key2": "Value2"}]})

7$mod

Find ({"key": {"$mod": [5,1]}})

8 not

Find ({"key": {"$not": {"$mod": [5,1]}}})

9 and

Find ({"$and": [{"x": {"$lt": 1}}, {"X": 4}]})

This match {"x" [0,4]}

Equivalent to find ({"X": {"$lt": 1, "$in": [4]}})

10 about null

Find ({"Y": null})---will not only find the Y:null document, but also find a document that does not exist for this field.

can use

Find ({"Z": {"$in": [null], "$exists": True}})

11 queries using regular expressions ---PCRE

Query named Joe or Joe

Find ({"name":/joe/i})

Find ({"name":/joe?/i})

Find ({"name":/^joe/})

12 Querying arrays

Find ({"Fruit": "value1"}) that is the array contains value1 can be searched!

The 13 array contains multiple

Find ({"fruit": {"$all": ["value1", "Value2"]}}

That is, the array contains both value1 and value2

14 Exact Match

Find ({"Fruit": "value1", "value2"]})

The array contains and only contains these 2 values!

15 ordinal query of an array

Find ({"fruit.2": "Peach"})

16$size

Find ({"fruit": {"$size": 3}})

17$slice

FindOne (criteria,{"comments": {"$slice": 10}})

Return to top 10 reviews!

FindOne (criteria,{"comments": {"$slice":-10}})

Return to post 10 reviews!

FindOne (criteria,{"comments": {"$slice": "23,10"}})

Skip 23 elements and return as many as 10 elements!

Note that only the comments can be controlled, and the other fields still return!

$

mode:

Comments:[{name:bob,email:xxx}, ..........]

Find ({"Comments.name": "Bob"},{"comments.$": 1})

Returns the first matching document.

19 query for array

{"X": {"$GT": Ten, "$lt": 20}}

If x is an array at this point, then any one element satisfies 1 of the criteria to be considered a match!

But if it's not an array, match it at the same time!

20$elemmatch

First, only the array elements will be matched

Find ({"X": {"$elemMatch": {"$GT": Ten, "$lt": 20}})

21 If an index is created

Find ({"X": {"$GT": Ten, "$lt": $}}). min ({"X": Ten}). Max ({"X": 20})

The effect is very good! Because not all indexes are scanned, they are scanned in part of the index!

22 Querying inline documents

Find ({"name": {"First": "Joe", "Last": "Schmoe"}})---exact match!

The tragedy is that this kind of query is also sequential related!!!

Note: For inline documents, such queries are exact matches and must be done in the following way!

23 Better practices

Find ({"Name.first": "Joe", "Name.last": "Schmoe"})

Is it the relationship of or???

24 Please refer to $elemmatch Method!

25 The ultimate must kill skill-where

----------------about Cursors

Take 100 or 4MB at a time, whichever is smaller.

Find (). Skip (3). Limit ({"Key1": 1, "Key2": -1}). _addspecial ("$maxscan", 20)

. _addspecial ("$min", 20)

. _addspecial ("$max", 20)

. _addspecial ("$showDiskLoc": TRUE)

. SnapShot ()---will make the query slow!

Note: You can specify--noscripting when starting Mongod, turn off JS Run! ---Close the server-side script!

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.