In the four CRUD operations, a record or document is obtained from the collection of mongodb databases. For non-DBAs, query is the most used. Therefore, if you have learned the query, it will be of great help for us to better operate the database in the future. You can use the following two methods to obtain a data or document from a collection in mongodb: find
In the four CRUD operations, a record or document is obtained from the collection of mongodb databases. For non-DBAs, query is the most used. Therefore, if you have learned the query, it will be of great help for us to better operate the database in the future. You can use the following two methods to obtain a data or document from a collection in mongodb: find
In the four CRUD operations, a record or document is obtained from the collection of mongodb databases. For non-DBAs, query is the most used. Therefore, if you have learned the query, it will be of great help for us to better operate the database in the future.
You can use the following two methods to obtain a data or document from a collection in mongodb:
Find ()
FindOne ()
Find () is the most important method for finding data from the database. The find () syntax is as follows:
Db. [set name]. find ( , )
Similar to SQL statements, query is equivalent to the query conditions in SQL, and projection is equivalent to the fields we need after SELECT in SQL. By default, find () returns a maximum of 20 records.
As described above, if we omit the query and projection parameters, the documents in the set will be returned by default.
Next, we will query the document whose name is user2 in the user set:
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 11}
{"_ Id": 2, "name": "user2", "age": 12}
{"_ Id": 3, "name": "user3", "age": 13}
{"_ Id": 4, "name": "user4", "age": 14}
{"_ Id": 5, "name": "user5", "age": 15}
{"_ Id": 6, "name": "user6", "age": 16}
{"_ Id": 7, "name": "user7", "age": 17}
{"_ Id": 8, "name": "user8", "age": 18}
{"_ Id": 9, "name": "user9", "age": 19}
{"_ Id": 10, "name": "user10", "age": 20}
> Db. user. find ({name: "user2"})/* we only use the query parameter. All fields are returned by default, similar to SELECT * from table */in SQL */
{"_ Id": 2, "name": "user2", "age": 12}
In the above Code, all fields in the document are returned. It may not matter if this document is small. For a large document, we only need one field. If this is returned, it will be a waste of resources. In this case, we can use the projection parameter.
We look for the document named user2 from the user table. We only want to know its age and do not need its name. We can look at the following:
> Db. user. find ({name: "user2" },{ age: 1 })
{"_ Id": 2, "age": 12}
In mongodb, non-zero values are regarded as true, and only 0 values are considered as false values. Therefore, true and false values can be used in the preceding case. In the code that comes from the end, I often use a non-zero number to represent the truth.
Someone may want to write the projection parameter as follows:
{Name: 0, age: 1}
In this case, the projection parameter in mongodb is incorrect, Or you only need to write the value you need (that is, the value is true ), if you do not want to write the value you do not want (All values are false), mixed use is not accepted.
FindOne () and find () are basically the same, but findONe () returns only the first piece of data no matter how many documents meet the query conditions. The syntax and usage of findOne () are the same as that of find.
4.2.1 query Selector
If mongodb only provides the query method described above, the powerful query function of mongodb is too well-known. Next we will learn the powerful query selector of mongodb.
In the document of mongodb, it is divided into comparison, logic, elements, javascript, geospatial, and data selector. We also follow this introduction.
Comparison Selector
Mongodb provides comparison selectors including $ all, $ gt, $ gte, $ lt, $ lte, $ in, $ nin, and $ ne.
$ All
Syntax: {field :{$ all :[ , ...]}
The search field value is an array and contains all documents with the given value.
> Db. phone. find ()
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e21820c9b0dc40419386"), "num": [4, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
> Db. phone. find ({num: {$ all: []})/* contains 1, 4, and no data */
> Db. phone. find ({num: {$ all: [1, 2]})
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
$ Gt
Syntax: {field :{$ gt: value }}
Find a document whose field value is greater than the given value.
> Db. user. find ({age: {$ gt: 6 }})
{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}
{"_ Id": ObjectId ("5198c286c686eb50e2c843ba"), "name": "user8", "age": 8}
{"_ Id": ObjectId ("5198c286c686eb50e2c843bb"), "name": "user9", "age": 9}
{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}
{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}
$ Gte
Syntax: {field :{$ gte: value }}
Find the document whose field value is greater than or equal to the given value.
> Db. user. find ({age: {$ gt: 6 }})
{"_ Id": ObjectId ("5198c286c686eb50e2c843b8"), "name": "user6", "age": 6}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}
{"_ Id": ObjectId ("5198c286c686eb50e2c843ba"), "name": "user8", "age": 8}
{"_ Id": ObjectId ("5198c286c686eb50e2c843bb"), "name": "user9", "age": 9}
{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}
{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}
$ Lt
Syntax: {field :{$ lt: value }}
Find a document whose field value is smaller than the given value.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({age: {$ lt: 5 }})
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
$ Lte
Syntax: {field :{$ lte: value }}
Find the document whose field value is less than or equal to the given value.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({age: {$ lte: 5 }})
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
$ In
Syntax: {field :{$ in :[ , ...]}
The query field value is an array and contains one or more documents with given values.
> Db. c2.find ()
{"_ Id": ObjectId ("519d4db8f90a441161408c2c"), "ary": [1, 2]}
{"_ Id": ObjectId ("519d4dbcf90a441_1408c2d"), "ary": [1, 3]}
{"_ Id": ObjectId ("519d4dc4f90a441_1408c2e"), "ary": [4, 5, 6]}
> Db. c2.find ({ary: {$ in: [1, 2]})
{"_ Id": ObjectId ("519d4db8f90a441161408c2c"), "ary": [1, 2]}
{"_ Id": ObjectId ("519d4dbcf90a441_1408c2d"), "ary": [1, 3]}
$ Nin
Syntax: {field :{$ nin :[ , ...]}
The query field value is an array and does not contain one or more documents with given values.
> Db. c2.find ()
{"_ Id": ObjectId ("519d4db8f90a441161408c2c"), "ary": [1, 2]}
{"_ Id": ObjectId ("519d4dbcf90a441_1408c2d"), "ary": [1, 3]}
{"_ Id": ObjectId ("519d4dc4f90a441_1408c2e"), "ary": [4, 5, 6]}
> Db. c2.find ({ary: {$ nin: [1, 2]})
{"_ Id": ObjectId ("519d4dc4f90a441_1408c2e"), "ary": [4, 5, 6]}
$ Ne
Syntax: {field :{$ ne: value }}
Search for documents where a field is not equal to a given value or does not exist.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({age: {$ ne: 1 }})
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({sex: {$ ne: 1})/* No sex field exists. Check all documents */
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
Logical Selector
As the name suggests, this set of selectors is related to logic, including $ and, $ not, $ or, and $ nor.
$ And
Syntax: {$ and :[{ },{ },...,{ }]}
Search for documents that meet all the given conditions. Here we use arrays to store all the conditions. $ And if the first condition is not met during execution, the short circuit will not continue execution.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({$ and: [{name: "user1" },{ age: 2}]})
> Db. user. find ({$ and: [{name: "user1" },{ age: 1}]})
{"_ Id": 1, "name": "user1", "age": 1}
$ And can be converted to common queries. For example, the preceding statements can be converted:
Db. user. find ({name: "user1", age: 1 })
Since both can be converted to a method query that does not apply $ and, why do we still need to use and? Because it is confusing to use a simple method to query if there are too many conditions or when combined with other selectors, it is clearer to use $ and.
$ Not
Syntax: {field :{$ not :{ }}}
Searches for documents that do not match the given query expression.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({age: {$ not: {$ gt: 5 }})/* search for documents whose age is not greater than 5 */
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
$ Or
Syntax: {$ or :[{ },{ },...,{ }]}
Search for documents that meet one of the expression arrays.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({$ or: [{name: "user1"}, {age: 10}]})/* find the document whose name is user1 or whose age is 10 */
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 10, "name": "user10", "age": 10}
$ Nor
Syntax: {$ nor :[{ },{ },...{ }]}
Search for documents that do not meet any expression in the given expression array.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
{"_ Id": 10, "name": "user10", "age": 10}
> Db. user. find ({$ nor: [{name: "user1"}, {age: 10}]})/* find an array where the name is not user1 or the age is not 10 */
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "name": "user6", "age": 6}
{"_ Id": 7, "name": "user7", "age": 7}
{"_ Id": 8, "name": "user8", "age": 8}
{"_ Id": 9, "name": "user9", "age": 9}
Element)
In the mongodb document, it is called an Element. It is always awkward to translate it into an Element selector, but no better one is found.
Such selectors include $ exists, $ mod, and $ type.
$ Exists
Syntax: {field :{$ exists: }}
Search for documents with a specified field. True indicates yes, and false indicates no.
> Db. c2.find ()
{"_ Id": 1, "name": "yue "}
{"_ Id": 2, "age": 2}
> Db. c2.find ({name: {$ exists: 1})/* search for documents with name fields */
{"_ Id": 1, "name": "yue "}
> Db. c2.find ({name: {$ exists: 0})/* search for documents without the age field */
{"_ Id": 2, "age": 2}
$ Mod
Syntax: {field :{$ mod: [pisor, remainder]}
Select a document where a Field Meets the modulo operation. The value of this field must be a number; otherwise, no results can be found.
> Db. c2.find ()
{"_ Id": 1, "name": "yue "}
{"_ Id": 2, "age": 2}
{"_ Id": 3, "age": 15}
> Db. c2.find ({age: {$ mod: [2, 1]})/* document about age modulo 2 and 1 */
{"_ Id": 3, "age": 15}
> Db. c2.find ({name: {$ mod: [2, 1]})
$ Type
Syntax: {field: {$ type: }}
Search for documents by field type. The numbers corresponding to each type are listed below.
Type |
No. |
Double Precision |
1 |
String |
2 |
Object |
3 |
Array |
4 |
Binary data |
5 |
Object ID |
7 |
Boolean Value |
8 |
Date |
9 |
Null |
10 |
Regular Expression |
11 |
JavaScript |
13 |
Symbol |
14 |
JavaScript (with range) |
15 |
32-bit integer |
16 |
Timestamp |
17 |
64-bit integer |
18 |
Minimum key |
255 |
Max keys |
127 |
In mongodbshell, some types do not exist. For example, the 32-bit precision of a number indicates double precision.
> Db. c2.find ()
{"_ Id": 1, "name": "yue "}
{"_ Id": 2, "age": 2}
{"_ Id": 3, "age": 15}
> Db. c2.find ({age: {$ type: 1})/* Documents with double age type */
{"_ Id": 2, "age": 2}
{"_ Id": 3, "age": 15}
Javascript Selector
Such selectors include $ regex and $ where.
$ Regex
Search for documents that meet the regular expression.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({name:/user */I })
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
The regex in Mongodb is compatible with perl, which is more convenient than the regular expression in the code above. Mongodb can also use the $ regex method. Example:
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({name: {$ regex: "user *", $ options: "I"})/* $ options set the regular option */
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
$ Where
$ Where can be used to search for satisfied documents based on expressions or functions. If true is returned when function is used, this document is found.
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({$ where: "this. sex = 'nan '"})
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({$ where: "obj. sex = 'nan '"})
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({$ where: function () {return this. sex = 'nan ';}})
{"_ Id": 6, "sex": "nan "}
> Db. user. find ({$ where: function () {return obj. sex = 'nan ';}})
{"_ Id": 6, "sex": "nan "}
Both this and obj in the above Code represent the current document.
Geographic location Selector
Refer to 4.5 geospatial search
Array Selector
The array selector operates on arrays, including $ elemMatch and $ size.
$ ElemMatch
The field value is an array, and the elements in the array are embedded documents. $ elemMatch is required for query based on embedded documents in the array.
> Db. c3.find ()
{"_ Id": 1, "array": [{"value1": 1, "value2": 0 },{ "value1": 2, "value2 ": 2}]}
> Db. c3.find ({array: {$ elemMatch: {value1: 1, value2: {$ gt: 1 }}}})
> Db. c3.find ({array: {$ elemMatch: {value1: 1, value2: {$ lt: 1 }}}})
{"_ Id": 1, "array": [{"value1": 1, "value2": 0 },{ "value1": 2, "value2 ": 2}]}
From the code above, we can see that $ elemMath loops through every embedded document in the array and compares it with the given conditions to find the documents that meet the conditions.
$ Size
Syntax: db. collection. find ({field: {$ size: }})
Find the array whose length meets the given value.
> Db. c4.find ()
{"_ Id": 1, "ary": [1]}
{"_ Id": 2, "ary": [2, 3]}
{"_ Id": 3, "ary": [4, 5, 6]}
> Db. c4.find ({ary: {$ size: 5})/* No document with an array length equal to 5 */
> Db. c4.find ({ary: {$ size: 2 }})
{"_ Id": 2, "ary": [2, 3]}