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

Query of MongoDB

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

MongoDB uses find to query and does not specify any parameters to indicate that it is a document query for the entire collection.

Find query

Specify the returned key

Sometimes it is not necessary to return all the key-value pairs in the document, but the desired key can be determined by the second parameter of find (or findOne), which not only saves the amount of data transferred, but also saves the time and memory consumption of the client decoding the document.

For example:

> db.people.find ()

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32

"enemies": 2}

{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}

>

If you are only interested in name and age, you can query it like this:

> db.people.find ({"name": "joe", "age": 21})

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

>

But "_ id" is always returned, even if it is not specified.

You can use the second parameter to weed out a key-value pair in the query results, for example, if you do not want to display the key-value pair of "friends": 32:

> db.people.find ({}, {"friends": 0})

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "enemies": 2}

{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}

>

Can also be used to prevent the return of "_ id":

> db.people.find ({"age": 21}, {"_ id": 0})

{"name": "joe", "age": 21}

2. Query conditions

Queries can match not only exactly, but also more complex conditions, such as ranges, OR clauses, or inversion.

2.1. Query conditions

Comparison operator for query criteria:

"$lt" corresponds to =

"$ne" corresponds to "unequal"

> db.people.find ({"age": {"$gte": 30, "$lte": 50}})

{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}

> db.people.find ({"age": {"$ne": 65}})

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32

"enemies": 2}

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}

"$ne" is used for all data types.

2.1By OR query

There are two ways to do OR queries.

"$in": can be used to query multiple values of a key

"$or": completes any given value of multiple key values.

2.1.1 "$in"

If there are multiple values matching a single key, add a conditional array with "$in":

> db.people.find ({"age": {"$in": [21pc49 in 65]}})

{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}

>

"$in" is flexible enough to specify different types of conditions and values:

> db.people.insert ({"age": "kkk"})

WriteResult ({"nInserted": 1})

> db.people.find ({"age": {"$in": [21, "kkk"]}})

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d334cbb174be28d017c1a6"), "age": "kkk"}

>

If the array corresponding to "$in" has only one value, then the effect is the same as directly matching this value.

> db.people.find ({"age": {"$in": [21]}})

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

> db.people.find ({"age": 21})

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

>

2.1.1 "$nin"

The opposite of "$in" is "$nin", which returns documents that do not match all the conditions in the array:

> db.people.find ({"age": {"$nin": [21pc49 nin 65]}})

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32

"enemies": 2}

{"_ id": ObjectId ("54d334cbb174be28d017c1a6"), "age": "kkk"}

>

2.1.1 "$or"

"$or" accepts an array of all possible conditions as a parameter:

> db.people.find ({"$or": [{"age": "kkk"}, {"friends": 32}]})

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32

"enemies": 2}

{"_ id": ObjectId ("54d334cbb174be28d017c1a6"), "age": "kkk"}

>

At the same time, "$or" can contain other conditional clauses:

> db.people.find ({"$or": [{"age": "kkk"}, {"friends": 32}, {"age": {"$in": [21min65]}}]})

{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32

"enemies": 2}

{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}

{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}

{"_ id": ObjectId ("54d334cbb174be28d017c1a6"), "age": "kkk"}

>

And-type queries always limit the scope of the results with as few conditions as possible, while OR-type queries, on the contrary, the first condition matches as many documents as possible, which is the most efficient.

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: 0

*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