Mongodb entry-7 query 2 array query $ all $ in $ all the elements of all Given Queries must be included in the array $ in. The following two codes can be viewed as long as the given query elements are included in the array.: [html] db. phone. find () {_ id: ObjectId (5198e20220c9b0dc40419385), num: [519, 3]} {_ id: ObjectId (
Mongodb entry-7 query 2 array query $ all $ in $ all the elements of all Given Queries must be included in the array $ in. The following two codes can be viewed as long as the given query elements are included in the array.: [html] db. phone. find () {_ id: ObjectId (5198e20220c9b0dc40419385), num: [1, 2, 3]} {_ id: ObjectId (519)
Mongodb entry-7 query 2
Array query $ all $ in
The $ all array must contain all the elements of the given query.
The $ in array can contain the given query element.
See the following two codes:
[Html]
> 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: [1, 2]})
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
> Db. phone. find ({num: {$ all: []}) ---> no data including
> Db. phone. find ({num: {$ in: []}) ---> contains 1 or 4 Data
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e21820c9b0dc40419386"), "num": [4, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
$ Exists: search for documents containing this field
A piece of code is as follows:
[Html]
> Db. phone. find ()
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e21820c9b0dc40419386"), "num": [4, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
{"_ Id": ObjectId ("5198e51a20c9b0dc40419388"), "state": 1}
> Db. phone. find ({state: {$ exists: 1}) --->
{"_ Id": ObjectId ("5198e51a20c9b0dc40419388"), "state": 1}
> Db. phone. find ({state: {$ exists: 0}) ---> document without the state field
{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}
{"_ Id": ObjectId ("5198e21820c9b0dc40419386"), "num": [4, 2, 3]}
{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}
$ Mod modulo operation
For example, we can perform some modulo operations when taking some data, such as the following documents with an age model of 3 or more:
[Html]
> Db. user. find ()
{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}
{"_ 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}
> Db. user. find ({age: {$ mod: [3, 1]})
{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b9"), "name": "user7", "age": 7}
{"_ Id": ObjectId ("5198c286c686eb50e2c843bc"), "name": "user10", "age": 10}
The statement db. user. find ("this. age % 3 = 1") can also be used to achieve the above effect, but it is not recommended.
$ Ne not equal is not equal
Search for users whose age is not equal to 0:
[Html]
> Db. user. find ()
{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}
{"_ 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}
> Db. user. find ({age: {$ ne: 0 }})
{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}
{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}
{"_ 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}
Some people refer to the method starting with $ as a magic method. It doesn't matter. This method is generally not in json, that is, it is generally used in, this is also the value of a field. it will be exposed later.