Mongodb entry-8 query 3

Source: Internet
Author: User
Tags mongodb query
Mongodb entry-8 query 3 continue to learn about mongodb query $ or $ nor $ or, or the meaning is identified as long as there is a match. $ nor is opposite to $ or, as long as $ or can be found as the removed part. [html] db. user. find () {_ id: ObjectId (5198c286c686eb50e2c843b2), name: user0, age: 0} {_ id

Mongodb entry-8 query 3 continue to learn about mongodb query $ or $ nor $ or, or the meaning is identified as long as there is a match. $ nor is opposite to $ or, as long as $ or can be found as the removed part. [html] db. user. find () {_ id: ObjectId (5198c286c686eb50e2c843b2), name: user0, age: 0} {_ id

Mongodb entry-8 query 3

Continue to learn about mongodb Query

$ Or $ nor

$ Or indicates that as long as there is a match, the. $ nor is opposite to $ or, as long as $ or can be found as the removed part.

[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 ({$ or: [{name: "user1" },{ age: 20}]}) --> here, we can see that the method starting with $ or is different from that starting with $, the rest is generally used as a key value, which serves as a key between them. It may not be very clear here, so you can understand it.

{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}

{"_ Id": ObjectId ("5198c3cac686eb50e2c843bd"), "name": "user0", "age": 20}

Similarly, $ nor is used to query other parts:

[Html]

> Db. user. find ({$ nor: [{name: "user1" },{ age: 20}]})

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}

{"_ 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}

$ Size: Query documents whose array length is equal to the given array Length

[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}

{"_ Id": ObjectId ("519969952b76790566165de2"), "num": [2, 3]}

> Db. phone. find ({num: {$ size: 4}) --> the result of the num array length of 4 is not

> Db. phone. find ({num: {$ size: 3}) --> there are three

{"_ Id": ObjectId ("5198e20220c9b0dc40419385"), "num": [1, 2, 3]}

{"_ Id": ObjectId ("5198e21820c9b0dc40419386"), "num": [4, 2, 3]}

{"_ Id": ObjectId ("5198e22120c9b0dc40419387"), "num": [1, 2, 5]}

$ Where custom Query

$ Where is a function. We can write this function and determine which values are needed. it cyclically scans the documents in the set and then executes the judgment in the function. If we return true, this document will be detected. however, the performance of this method is not very good. For example, I can obviously feel a pause in the following query (compared with the method mentioned above). If you are interested, try it yourself. we recommend that you use $ where to query only when other $ methods cannot meet the query requirements.

[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 ({$ where: function () {return this. age = 3 | this. age = 4 }})

{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}

$ Type query by Data type

Each data type in mongodb has corresponding numbers. We need to use these numbers when using $ type. The document provides the following representation:

Type Number

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 a range) 15

32-bit integer 16

Timestamp 17

64-bit integer 18

Minimum key 255

The maximum key is 127. Example code:

[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}

{"_ Id": ObjectId ("51996ef22b76790566165e47"), "name": 23, "age": 33}

> Db. user. find ({name: {$ type: 1}) --> search for documents with double-precision name

{"_ Id": ObjectId ("51996ef22b76790566165e47"), "name": 23, "age": 33}

Some type values in the command line do not work, which may be related to the command line environment. If anyone knows this, please leave a message for me.

Regular Expression

Mongodb also supports regular expressions, which are basically the same as those in javascript, but is not recommended because of poor performance.

[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}

{"_ Id": ObjectId ("51996ef22b76790566165e47"), "name": 23, "age": 33}

> Db. user. find ({name:/user */I}) --> query documents whose names start with user and are case-insensitive.

{"_ 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}

Sort

In mongodb, sorting is very simple. Use the sort method to pass it to the field you want to sort by which method. Here 1 indicates ascending, and-1 indicates descending.

[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}

> Db. user. find (). sort ({age: 1 })

{"_ 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}

> Db. user. find (). sort ({age:-1 })

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 5}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 4}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 3}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b3"), "name": "user1", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b2"), "name": "user0", "age": 0}

Group Query

The group in mongodb can implement grouping functions similar to those in relational databases. However, the group in mongodb is far more powerful than the group in relational databases and can implement the map-reduce function. For map-reduce readers, Baidu, it is quite popular now.

The json parameters in the group are similar to {key: {field: 1}, initial: {Variable: initial Value}, $ reduce: function (doc, prev) {function Code }}.

The fields represent the fields to be grouped. variable indicates the variables used in this group and an initial value is given. it can be used in the subsequent $ reduce function. the two parameters of $ reduce represent the result of the function execution in the current document and the previous document respectively. the following table lists the number of users of the same age by age group:

[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": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 2}

> Db. user. group ({key: {age: 1}, initial: {count: 0}, $ reduce: function (doc, prev) {prev. count ++ }})

[

{

"Age": 0,

"Count": 1

},

{

"Age": 1,

"Count": 3

},

{

"Age": 2,

"Count": 2

}

]

For more information about group, see http://www.2cto.com/database/201305/212159.html.

Distinct

Removing duplicate data in the query results does not affect the original data. The returned results are an array.

[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": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 2}

> Db. user. distinct ("age ")

[0, 1, 2]

Paging Query

Implementing paging in mongodb is relatively simple. You need to use the skip and limit methods. skip indicates that the previous documents are skipped, and limit indicates that several documents are displayed.

[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": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b7"), "name": "user5", "age": 2}

> Db. user. find (). skip (2 ). limit (3) --> skip the first two documents to query the following three documents. After testing, the sequence of use of the two methods is not affected.

{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 1}

> Db. user. find (). limit (3). skip (2)

{"_ Id": ObjectId ("5198c286c686eb50e2c843b4"), "name": "user2", "age": 2}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b5"), "name": "user3", "age": 1}

{"_ Id": ObjectId ("5198c286c686eb50e2c843b6"), "name": "user4", "age": 1}

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.