Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongo Advanced query

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Conditional operator

(1) conditional operator

Db.collection.find ({"field": {$gt: value}}); / / greater than: field > value

Db.collection.find ({"field": {$lt: value}}); / / less than: field

< value db.collection.find({ "field" : { $gte: value } } ); // 大于等于: field >

= value

Db.collection.find ({"field": {$lte: value}}); / less than or equal to: field db.user.find ()

{"_ id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}

{"_ id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}

{"_ id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}

> db.user.find ({age: {$mod: [6jue 1]}})

{"_ id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}

(5) $ne is not equal to

Query data where the value of age is not equal to 7

> db.user.find ({age: {$ne:6}})

{"_ id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}

{"_ id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}

> db.user.find ({age: {$ne:7}})

{"_ id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}

{"_ id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}

(6) $in contains

Query data with a value of age in the range of 7 and 8

> db.user.find ({age: {$in: [7 in 8]}})

{"_ id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}

{"_ id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}

(7) min does not contain

Query data whose value of age is outside the range of 7 and 8

> db.user.find ({age: {$nin: [7 nin 8]}})

{"_ id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}

(8) number of elements in the $size array

Match db.users.find ({favorite_number: {$size: 3}})

Mismatch db.users.find ({favorite_number: {$size: 2}})

> db.user.find ({favorite_number: {$size:3}})

{"_ id": ObjectId ("579eee3d8aeb5abda0d54b5e"), "name": "David", "age": 26, "favorite_number": [6, 7, 9]}

> db.user.find ({favorite_number: {$size:2}})

>

(9) regular expression matching

Query data for which name does not begin with T

Db.user.find ()

{"_ id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "age": 20}

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

> db.user.find ({name: {$not:/ ^ T. * /}})

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

>

(10) Javascript query and $where query

Query data with a greater than 3. The following query methods go the same way.

Db.c1.find ({a: {$gt: 3}})

Db.c1.find ({$where: "this.a > 3"})

Db.c1.find ("this.a > 3")

F = function () {return this.a > 3;} db.c1.find (f)

(11) number of count query records

> db.user.find ()

{"_ id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "age": 20}

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

> db.user.find () .count ()

two

>

(12) skip limit returns the starting point of the record

Query item 2 data in user

> db.user.find ()

{"_ id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "age": 20}

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

> db.user.find () .skip (1) .limit (1)

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

>

(13) sort sorting

Query C1 table is sorted in ascending order of age

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

{"_ id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "age": 20}

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

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

{"_ id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "age": 10}

{"_ id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "age": 20}

>

(14) Vernier

For (var iTunes 1) for (var c=db.user.find (); c.hasNext ();) {printjson (c.next ());}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b5f"), "x": 1}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b60"), "x": 2}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b61"), "x": 3}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b62"), "x": 4}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b63"), "x": 5}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b64"), "x": 6}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b65"), "x": 7}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b66"), "x": 8}

{"_ id": ObjectId ("579ef6198aeb5abda0d54b67"), "x": 9}

>

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 232

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report