Use the Find or FindOne function to execute a query in MongoDB
Find function
Db.c.find ()--query collection C all
Db.c.find ({"Name": "Zhangsan"})
Note: The value of a query condition must be a constant, that is, you cannot use a variable
Db.c.find ({"Name": THIS.name})//This won't work.
Query criteria
"$lt" Correspondence <
"$lte" corresponds to <=
"$GT" Correspondence >
"$gte" corresponds to >=
"$ne" corresponds to! =
Usage:
Db.users.find ({"Age": {"$gte": +, "$lte": 30}})//Find users older than or equal to 18 less than or equal to 30
Db.users.find ({"name": {"$ne": "Zhangquan Egg"}})
or query
MongoDB in two ways to implement or query:
The "$in" can be used to query multiple values of a key, which is flexible enough to specify different types of conditions
"$nin" is opposite to "$in" action
"$or" can query for any given value in more than one base
Usage:
Db.users.find ({"name": {"$in" ["Wang Nima", "Zhangquan Egg"]}})
Db.users.find ("user_id": {"$in": [123456, "123456"]})//lookup when the data type of the user ID key is changed
Db.users.find ({"name": {"$or": ["Age": {"$in": [18,20,25]},{"Gender": 1}]}})
$not
$not is a meta-conditional operator that can be used on top of any other condition.
Db.users.find ({"Id_num": {"$not": {"$mod": [5,1]}})//"$mod" is the modulo operator
Null type
The Users collection has three data as follows:
{"Name": Wang Nima "," Age ": 18}
{"Name": "Zhangquan Egg", "Age": 20}
{"Name": "Tangma", "Age": null}
Where the age of "Tangmaru" is a null value
Then query: Db.users.find ({"Age": null})
The result is not only {"name": "Tangma", "Age": null}, the other two will be detected. Because NULL will not only match a document with a key value of NULL, but also
Matches a document that does not contain this key.
If you only want to match a document with a key value of NULL, you need to use "$exists"
Db.users.find ({"Age": [null], "$exists": true})
Regular expressions
Db.users.find ({"name":/Zhang./})
Querying arrays
Db.food.insert ({"Fruit": ["apple", "banana", "Peach"]})
Inquire:
Db.food.find ({"Fruit": "Banana"})
The result will match the document inserted above
$all: Used to find arrays that match multiple conditions
For example, a common collection that contains 3 elements:
Db.food.insert ({"Fruit": ["apple", "banana", "Peach"]})
Db.food.insert ({"Fruit": ["apple", "kumquat", "Orange"]})
Db.food.insert ({"Fruit": ["Cherry", "banana", "Apple"]})
To find documents that have both "apple" and "banana":
Db.food.find ({fruit:{$all: ["Apple", "Banana"]}})
The results will find the 1th and 3rd documents
$size: An array for querying a specific length
Db.food.find ({"Fruit": {"$size": 3}})
Match by index: Db.food.find ({"fruit.2": "Peach"]})
$slice: The result set used to split the Find/findone return value, returning a subset of the array elements that a key matches.
Db.food.findOne ({},{"fruit": {"$slice": 2}})
Db.food.findOne ({},{"fruit": {"$slice":-2}})//Right-to-left
$elemMatch: Requires MongoDB to use query conditions simultaneously on the same array (by default multiple conditions are applied to multiple arrays)
Db.test.find ({"x": {"$elemMatch": {"$GT": Ten, "$lt": 20}})
$where Query
$where used for any query, it can execute arbitrary JavaScript. Therefore, for safety reasons, $where should be strictly or not used.
Db.food.find ({"$where": function () {Forvar current in this () { //todo}}})
Using $where queries is much slower than regular queries because it converts each document from a Bson object to a JavaScript object.
Limit, skip, and sort
Limit function: For limiting the number of query results
Db.food.find (). Limit (3)//returns only 3 matching results. If the result of the match is less than 3, the result of the matching quantity is returned.
Skip function: Used to skip a specified number of documents
Db.food.find (). Skip (1)//Skip 1th document, return the second two
Sort function: Sorts the results of the query, 1 is ascending,-1 is descending
Db.food.find (). Sort ({"Fruit": 1})
MongoDB Learning Notes-Query