Precisely match a single key-Value Pair: {age: 28}, and return all documents whose age value is 28. Multiple key-value pairs: {username: tom, age: 28}
Precisely match a single key-Value Pair: {age: 28}, and return all documents whose age value is 28. Multiple key-value pairs: {username: tom, age: 28}
Exact match
For a single key-Value Pair: {"age": 28}, all documents whose "age" value is 28 are returned.
Multiple key-value pairs: {"username": "tom", "age": 28} combine multiple query conditions, equivalent: condition 1 AND condition 2 AND... AND condition N. This query document returns all documents whose username is tom and their age is 28.Condition matching range
"$ Lt", "$ lte", "$ gt", "$ gte"
Comparison operators:
<$ Lt <= $ lte> $ gt >=$ gte
You can combine them to find values in a range.
For example, the query documents between 18 and 35 are: {"age": {"$ gte": 18, "$ lte": 35 }}
This range query method is especially convenient for date type values.
"$ Ne"
"Not equal" operator, corresponding :! =
If the user name is not tom, the query document is: {"username": {"$ ne": "tom "}}OR Query
$ In
Queries multiple values corresponding to a key and performs OR queries on a single key.
For example, if the number of winning events is or 8, you need to find out the query documents for all the winning data: {"ticket_no": {"$ in": [1, 4, 8]}
$ Nin
Correspondingly, $ nin returns data that does not match the values in the array. For example, the query document for finding data that does not win the prize is: {"ticket_no": {"$ nin": [1, 4, 8]}.
$ Or
It is more common to query any value of multiple keys. It accepts an array of all possible conditions as a parameter, or it can contain other conditions. For example:
{"$ Or": [{"ticket_no": {"$ in": [1, 4, 8] },{ "winner": true}]}
The first condition of $ or should be matched to more documents as much as possible, so as to be the most effective.$ Not
$ Not is a metaconditional sentence. It can be used on any other condition and the table is reversed.
For example: Query document: {"value": {"$ mod": [5, 1]}. The values that meet the criteria include: 1, 6, and 11.
To search for data with values of 2, 3, 4, 5, 7, 8, 9, 10, and 12, you can use: {"value": {"$ not ": {"$ mod": [5, 1]}.
$ Exists
Used to query whether a key exists in the document. For example, find the document with the key name key1 not found: {"key1": {"$ exists": false }};
Conversely, {"key1": {"$ exists": true} indicates that the key1 key exists.
Type match Null
Query the document {"x": null}. After the statement is executed, it returns the document containing the key-Value Pair "x": null, and the document without the x key.
Regular Expression
All the regular expressions supported by PCRE can be accepted by MongoDB.
If the query document {key1 "} is returned.
It can be used in like scenarios in SQL.
Query Arrays
Each element in the array is the value of the entire key. If any:
Document 1: {"fruit": ["apple", "pear", "peach"]}, document 2: {"fruit": ["peach", "banana ", "apple"]}, document 3: {"fruit": ["orange", "banana", "apple"]},
Single element matching
If the query document is {"fruit": "apple"}, documents 1, 2, and 3 are matched successfully.
Match Multiple Elements
The $ all clause is required. If the query document is {"fruit": {"$ all": ["apple", "peach"]}, the document 1 and 2 will be matched and irrelevant to the element sequence.
Exact match
If the query document is: {"fruit": ["apple", "pear", "peach"]}, only document 1 is matched. for missing or redundant documents, and those with different orders won't match.
Subscript matching
The key. index method is used, and the array subscript starts from 0. For example, if you query the document {"fruit.2": "apple"}, the document 2 and 3 will be matched.
Length matching
If the query document is: {"fruit": {"$ size": 3 }}, it indicates an array with a length of 3. the query documents 1, 2, and 3 will be matched.Query of embedded documents
Point representation
For example:
{"_ Id": 1, "name": {"first": "Joe", "last": "Smith "}}
You can query: {"name. first": "Joe", "name. last": "Smith "}
$ ElemMatch
It is used when multiple key operations are required for an embedded document.
If any:
{"Comments": [{"name": "Tom", "score": 3, "comment": "bad" },{ "name": "Jim ", "score": 6, "comment": "good"}]}
To search for comments whose Tom scores are greater than 5, you can only: {"comments": {"$ elemMatch": {"name": "Tom", "score ": {"$ gt": 5 }}}}
But it cannot: {"comments": {"name": "Tom", "score": {"$ gt": 5 }}. it cannot match the "comment" key.
Or {"comments. name": "Tom", "comments. score": {"$ gt": 5}, not the same comment.