In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how MongoDB queries arrays, embedded documents, and $where. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
[query array]
Querying an array is easy. For an array, we can understand it this way: each element in the array is a valid value of this key-value pair, as in the following example: we want to query the fruit store that sells apple:
We found that any array containing apples can be queried. If you want to match an array through multiple elements, you need the conditional operator "$all". For example, we want to query a fruit store that sells both apple and banana:
We see that using "$all" does not require the order of the elements in the array, as long as all the contained arrays can be queried. Array queries can also use exact matching, that is, the values of key-value pairs in the query condition document are also arrays, such as:
If it is an exact match, MongoDB is handled in exactly the same way, that is, the order and quantity should be the same. The order of the first document and the query condition is inconsistent, and the third document has one more element than the query condition document, which has not been matched successfully!
For array matching, another form is to precisely specify the matching of elements at a certain position in the array. As we mentioned earlier, the index in the array can be used as a key. For example, if we want to match the fruit store, the second fruit is the fruit store that sells orange:
The array index starts at 0, and we use furits.1 as the key to match the second fruit.
The "$size" conditional operator can be used to query an array of specific lengths, for example, we want to query fruit stores that sell three kinds of fruits:
However, the conditional operator "$size" cannot be used with other operators such as "$gt", which is a flaw in this operator. Using this operator, we can only accurately query an array of a certain length. If in practice, when querying an array, you need to query according to its length range, the recommended practice here is to add an additional "size" key in this document to record the size of the array, and add 1 to the "size" key while performing the "$push" operation on the array. As follows:
But this method doesn't work with the modifier "$addToSet" because you can't tell if this element has been added to the array!
As mentioned in the previous article, the second parameter of the find function is used to query which keys are returned, and it can also control the query to return a subarray of the array, such as the following example: I only want to query the first two of the array sold by the fruit store:
"$slice" can also be intercepted from behind, using the plural, such as-1 to intercept the last one; you can also intercept the middle part, such as [2prime3], that is, skip the first two, intercept 3, and return all if there are less than 3 left!
If a key in the second parameter uses the conditional operator "$slice", the default query returns all keys. If you want to ignore which keys at this time, you can specify them manually! Such as:
[query embedded document]
There are two ways to query documents, one is to fully match the query, and the other is to query for key-value pairs! The exact matching query of an embedded document is the same as that of an array, the number of key-value pairs in the embedded document must be the same in order to match, as in the following example:
Queries against specific key-value pairs of embedded documents are the most commonly used! Use dot representation to accurately represent the keys of an embedded document:
We see, this query, all the valid documents have been queried! Through dot representation, you can represent keys that go deep into the interior of an embedded document! Use "dot representation" to query embedded documents, which also restricts that no key can contain "." when inserting a document!
When an embedded document becomes complex, such as the value of a key is an array of embedded documents, the matching of embedded documents requires some skills, such as the following example:
We want to query the blog document called "joe" and its score is more than 5 points, but it is problematic for us to use "dot representation" to write directly, because this document has two comments, one author's name is "joe" but the score is only 3, the author's name is "jimmy", but the score is 5! That is, this query condition matches different documents in the array! This is not what we want, we are here to use a set of conditions instead of specifying each key individually, using the conditional operator "$elemMatch"! He can limit a set of conditions to the matching of a single document in an array:
In doing so, the result is correct! The use of the conditional operator "$elemMatch" can combine a set of conditions, and can also achieve the effect of fuzzy query of "dot representation"!
[$where]
The query mode of all the key-value pairs mentioned above, we can also see that it is already very powerful! But if you really encounter a situation that can't be implemented in the above way, don't panic, MongoDB provides us with the ultimate weapon: "$where", with which he can execute any JavaScript as part of the query! The most typical application: a document is selected if the values of two keys are equal, otherwise it is not selected:
We can see that using "$where" is actually writing a javascript function. When querying, MongoDB will convert each document into a javascript object, and then throw it into this function to execute, and determine whether it matches by returning the result! In practical use, try to avoid using the "$where" conditional operator because of its poor performance! In the process of execution, you need to convert each file into a javascript object! If it's inevitable, try to write find ({"other": ".", "$where": ""}), putting "$where" at the end, tuning as a result, and using regular queries as prefilters! This can reduce some performance loss!
We can also find here that the "$where" conditional operator is also used as the key of the outer document, and yesterday it was said that the "$or" conditional operator is used as the key of the outer document. The rest of the conditional operators encountered so far are used as keys to the inner document!
Thank you for reading! On "MongoDB how to query arrays, embedded documents and $where" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.