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

How to get started with MongoDB (2)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

3. Processing of null in query

The matching of null is interesting: null matches not only documents where the value of the specified key is indeed equal to null, but also documents where the key does not exist in the query. For example, insert the following three documents:

> db.users.insert ({"name": "Tom", "age": 20, "job": "Sales"}); > db.users.insert ({"name": "Sam", "age": 25, "job": "Manager"}); > db.users.insert ({"name": "Jim", "age": 25}); > db.users.insert ({"name": "Sam", "age": 25, "job": null})

Now let's match the document whose job is null in the traditional way:

> db.users.find ({"job": null})

The results are as follows:

{"_ id": ObjectId ("4f0714e3edb28db4864be582"), "name": "Jim", "age": 25} {"_ id": ObjectId ("4f071542edb28db4864be583"), "name": "Sam", "age": 25, "job": null}

Obviously, you match not only documents where job is indeed null, but also documents that do not have the key job. Most of the time, this is not our original intention. There is a $exists operator to solve this problem. Look at the code first:

Db.users.find ({"job": {"$in": [null], "$exists": true}})

The results are as follows:

{"_ id": ObjectId ("4f071542edb28db4864be583"), "name": "Sam", "age": 25, "job": null}

4. Regular expression

MongoDB uses an Perl-compatible library of regular expressions to match regular expressions. The regular expression itself is very powerful, and there are books dedicated to this. I don't aim at this here, but just give a simple example. For example, ignore case to match documents whose name is Tom:

Db.users.find ({"name": / tom/i})

5. Array query

The big idea of array query: in most cases, each element of the array can be the value of the corresponding key. This must be hard to understand. For example, insert the following document:

Db.food.insert ({"fruit": ["apple", "banana", "peach"]})

Now execute the following three queries, all of which will match this document.

Db.food.find ({"fruit": "apple"}); db.food.find ({"fruit": "banana"}); db.food.find ({"fruit": "peach"})

If you need to match a document with both apple and banana for the value of the fruit key, you can use the $all match. Now let's assume the following documents:

{"_ id": ObjectId ("4f071a10edb28db4864be584"), "fruit": ["apple", "banana", "peach"]} {"_ id": ObjectId ("4f071c5676285076f80ca7c7"), "fruit": ["apple", "banana"]} {"_ id": ObjectId ("4f071c6076285076f80ca7c8"), "fruit": ["apple"]} {"_ id": ObjectId ("4f071ce576285076f80ca7c9") "fruit": ["banana", "apple"]}

Execute the following query:

Db.food.find ({"fruit": {"$all": ["apple", "banana"]}})

You can match the following three documents:

{"_ id": ObjectId ("4f071a10edb28db4864be584"), "fruit": ["apple", "banana", "peach"]} {"_ id": ObjectId ("4f071c5676285076f80ca7c7"), "fruit": ["apple", "banana"]} {"_ id": ObjectId ("4f071ce576285076f80ca7c9"), "fruit": ["banana", "apple"]}

You might say that you can exactly match the entire array as follows:

Db.food.find ({"fruit": ["apple", "banana"]})

The result is only one document:

{"_ id": ObjectId ("4f071c5676285076f80ca7c7"), "fruit": ["apple", "banana"]}

Not surprisingly, this is an exact match, and even the order in which apple and banana appear cannot be reversed. This result is obviously not what we want a lot of times. That's why the $all matching operator was introduced.

There is also a matching syntax: you can use key.index to match the index + 1 element value of the specified key corresponding value (in the case of an array). Again, in the example of the fruit above, let's assume that to match a document with the corresponding value of the result key and the third element peach, we can write:

Db.food.find ({"fruit.2": "peach"})

Note that the array subscript starts at 0.

If we need a document that matches the value array of the fruit key with three elements, we can use the $size match operator:

Db.food.find ({"fruit": {"$size": 3}})

This also matches the first document. It is important to note, however, that the $size match cannot be used with other query criteria. For example, if you write as follows, there will be no result:

Db.food.find ({"fruit": {"$gt": {"$size": 2})

This is a document that does not match the value array of the fruit key with more than 2 elements. There will be no query results in this way.

"MongoDB:The Definitive Guide"

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