Questions about MongoDB query (1)

Source: Internet
Author: User
This article is a blog post from MongoDB core developer @ kchodorow. It is the first article about MongoDB query puzzles. It introduces some query rules and usage of range query in Array through several examples. Assume that a Collection contains the following data: {x:-5} {x: 0} {x: 5} {x: 10} {x: [0, 5]} {x :[

This article is a blog post from MongoDB core developer @ kchodorow. It is the first article about MongoDB query puzzles. It introduces some query rules and usage of range query in Array through several examples. Assume that a Collection contains the following data: {"x":-5} {"x": 0} {"x": 5} {"x ": 10} {"x": [0, 5]} {"x ":[

This article is a blog post from MongoDB core developer @ kchodorow. It is the first article about MongoDB query puzzles. It introduces some query rules and usage of range query in Array through several examples.

Assume that a Collection contains the following data:

{"x": -5}{"x": 0}{"x": 5}{"x": 10}{"x": [0, 5]}{"x": [-5, 10]}{"x": [-5, 5, 10]}

What data will be returned if we execute the following query statement on this Collection?

db.foo.find({"x" : {"$gt" : -1, "$lt" : 6}})

The result is as follows:

{"X": 0} {"x": 5} {"x": [0, 5]} {"x": [-5, 10]} // This item is also found !! {"X": [-5, 5, 10]}

The preceding red comment indicates that both of the elements meet the conditions greater than-1 and less than 6. Why is it found? Let's take a look at the principle of MongoDB Range Query on Array.

When performing a range query, MongoDB can be understood as comparing according to various conditions. For the row in question,-5 in its Array is smaller than 6, and 10 is greater? -1, so the system will think that this line meets both of these conditions (because the system does not check whether the same element of Array meets these two conditions ).

To satisfy each condition on the same element, we can use the $ elemMatch operator. For example, if we modify the preceding query statement, the following result is obtained:

> db.foo.find({x: {$elemMatch: {$gt : -1, $lt : 6}}}){"x" : [0, 5]}{"x" : [-5, 5, 10]}

The data in the red column is gone, but wait a moment, there are still two pieces of data. Why? The two lost data are the following:

{"x": 0}{"x": 5}

We can see that the common characteristics of the two are that the value corresponding to x is not an Array. This is why the two are lost, because the $ elemMatch operator only filters the Array. If the value is not an Array, it is directly considered as not meeting the condition.

The preceding two query methods have a working principle, but none of them have the expected results. Is there any way to get the expected query results? The answer is yes. At this time, we need to combine the min () and max () methods.

Let's talk about the min () and max () methods. These two methods act on the MongoDB query cursor. They can guide the MongoDB query using indexes. min (-1) this means that when using this index, MongoDB only uses an index greater than-1, while max (6) means that when using this index, only indexes smaller than 6 parts are used. For example, if we use the following method, we can successfully filter out a row of data with doubts in red.

> db.foo.find({"x" : {"$gt" : -1, "$lt" : 6}}).min({"x" : -1}).max({"x" : 6}){"x" : 0}{"x" : [ 0, 5 ]}{"x" : 5}{"x" : [ -5, 5, 10 ]}

Have Fun!

Reference: www.kchodorow.com

Zone 42 VPS

The 42qu.com cloud host is sold to entrepreneurs. Click here to view details


Warning: Call_user_func_array () expects parameter 1 to be a valid callback, function 'embed _ rssfooter 'not found or invalid function name in /Home/b55/htdocs/blog.nosqlfan.com/wp-appsdes/plugin.phpOn line 166

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.