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 match Range
- "$ Ne"
"Not equal" operator, corresponding :! =
If the user name is not tom, the query document is: {"username": {"$ ne": "tom "}}
OR Query
$ 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: {"key1": {"$ exists": false} that does not exist }};
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
- $ 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.