Mongodb entry-7 query 2

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.