Get the App
SLTechnology News&Howtos  ›  Database  › 

An example Analysis of the $elemMatch problem in the MongoDB operator

Shulou Source: shulou.com Published: 2022-05-31 21:57:49 09月19日 Update

Editor to share with you the example analysis of the $elemMatch problem in the MongoDB operator, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

problem

If there is only one record in the MongoDB database collection

{"_ id": ObjectId ("5e6b4ef546b5f44e5c5b276d"), "name": "Zhao Xiaoming", "used_name": ["Zhao Ming", "Zhao Xiaopeng"], "age": 16, "gender": 0, "relatives": [{"name": "Zhao Gang", "relationship": 0}, {"name": "Xiuying" "relationship": 1}]}

We execute the query

Db.getCollection ('Persion') .find ({"relatives.name": "Zhao Gang", "relatives.relationship": 1})

Will you get the results at this time?

At first I took it for granted that there would be no results, but the results often ran counter to expectations.

What, I was confused for a moment. Don't the query results of Mongo have to meet all the conditions?

Analysis.

Disbelieving in evil, I tried the favorite Xiaobai query again.

Db.getCollection ('Persion') .find ({"name": "Zhao Xiaoming", "age": 18})

This time the result is empty, well, is this the Mongo I am familiar with?

So what's the difference between the two queries? There are two differences.

Whether it is a secondary field

Whether it is an array

So let's change the data to

{"_ id": ObjectId ("5e6b4ef546b5f44e5c5b276d"), "name": "Zhao Xiaoming", "used_name": ["Zhao Ming", "Zhao Xiaopeng"], "age": 16, "gender": 0, "relative": {"name": "Zhao Gang", "relationship": 0}}

Continue to execute the query

Db.getCollection ('Persion') .find ({"relatives.name": "Zhao Gang", "relatives.relationship": 1})

The result is an empty set

Next, try to query.

Db.getCollection ('Persion') .find ({"relatives.name": "Zhao Gang", "relatives.relationship": 0})

You can get a result this time.

Through the above two queries, we can basically eliminate the influence of secondary fields.

That's the reason for the array, so why exactly?

Restore the data to its original format and continue with different queries

Db.getCollection ('Persion') .find ({"relatives.name": "Zhao Gang", "relatives.relationship": 2})

The result is an empty set

Then we can conclude that for array fields, each query condition only needs one item in the array to satisfy the condition, not that there must be one item in the array that satisfies all the query conditions.

So what should I do if I want to achieve the latter effect?

Solve

At this point, we need to use today's protagonist $elemMatch, which is officially defined as follows:

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

{: {$elemMatch: {,.}

If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.

You cannot specify a $where expression in an $elemMatch.

You cannot specify a $text query expression in an $elemMatch.

We can change the query above to

Db.getCollection ('Persion') .find ({"relatives": {"$elemMatch": {"name": "Zhao Si", "relationship": 0})

You can get the results at this point, but

Db.getCollection ('Persion') .find ({"relatives": {"$elemMatch": {"name": "Zhao Si", "relationship": 1})

The result is an empty set

The above is all the content of the article "sample Analysis of $elemMatch problems in the MongoDB operator". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Query result Zhao Gang array condition question analysis field data article Xiao Ming empty set operator example different content Zhao Ming try confused like to see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux NVidia Xiaomi Shulou Technology