MongoDB (v): MongoDB additions and deletions (4)----query details (below)

Source: Internet
Author: User

(1) $all query: Used to query an array, match elements in an array, to match all to have results: example:

Now if I write this way: Db.customer.find ({"Books": {"$all": ["Java", "JQuery"]}}), then the result is not found, because

in document no results are matched, only the ["Java", "JQuery"] are matched in the document to be queried. It is similar to the in SQL statement,

but you have to meet all the conditions.

(2) When querying an array, this is also possible to isolate the result: Db.customer.find ({"Books": "Java"}), as follows:

I have always felt strange, obviously is the array, how to use this way to match also can succeed? Nondescript. Let's just say this: In the array, each element is the entire

The value of the key.

Of course, querying all of the array elements is certainly possible. As follows:

However, please note: it must be accurate to match the line, written like this: Db.customer.find ({"Books": ["Java", "C"]}) is not find the results, written like this:

Db.customer.find ({"Books": ["Java", "C + +", "C"]}) is also not able to find the results, that is, the sequence can not be changed. This further deepened my question, how

? Db.customer.find ({"Books": "Java"}) so that's the right thing to do? Remember this first.

(3) Index usage: A question: What should I do if I want to query the document of the second book C now? You can now use the subscript for the array, as follows:

The array subscript is starting from 0. Books.1 represents a second element

(4) $size: Query the array of the specified length, for example: now to query the records of 3 books, you can: Db.customer.find ({"Books": {"$size": 3}})

One thing to note: $size can only be accurate query, can not do scope query.

(5) $silce query: Returns a subset of an array, one can see:

Description: Db.customer.find ({"_id": 1}, {"Name": 1, "books": {"$slice": 1}}): Displays the first element of the array (the more accurate point should be the explicit

shown Array the first few elements, starting with an element. That is, if {"$slice": 2}), the result is: ["Java", "C"].

Db.customer.find ({"_id": 1}, {"Name": 1, "books": {"$slice": []}}): Displays the elements of the array interval 1--2

Db.customer.find ({"_id": 1}, {"Name": 1, "books": {"$slice":-1}}): Displays the last element of the array (the more accurate point should be the number of the display.

Group the last few elements). Look at the following to understand:

(6) Query the embedded document:

Prepare data: {"_id": 1, "name": "Zhangsan", "address": {"Province": "Hunan", "City": "Changsan"}}, now C

I want to find province data for Hunan, which can be done using "." To find: The following:

Db.customer.find ({"Address.province": "Hunan"})

Note: Db.customer.find ({"Address": {"Province": "Hunan"}}) does not find data unless written in the form of an exact match:

Db.customer.find ({"Address": {"Province": "Hunan", "City": "Changsan"}}), so that data can be detected. The former cannot find out

The data is also well understood, "address" This key corresponds to the value is {"Province": "Hunan", "City": "Changsan"}, instead of {"Province": "Hunan"},

The same db.customer.find ({"Address.province": "Hunan"}) can detect the data because the "address.province" key corresponds to the value of

Is "Hunan".

Note: An absolute match is an order that must not be chaotic. You cannot have a comparison operator.

(7) $elemMatch query: An embedded document that is more complex than the above: Prepare the data as follows:

{



    "Grade": /span>
    [

    {" Course ":" Chinese "," score ": +," flag ": 2},
    {"Course": "中文版", "Score": 3}
   ]
}

Now I want to find out course for math,score more than 60 records, what should I do? It is supposed that the above record is not found, because it does not meet the requirements.

The first idea might be this:

Db.stu.find ({"Grade": {"course": "Math", "score": {"$GT": 60}}), this is certainly not possible, for the reasons stated above.

What about this?

Db.stu.find ({"Grade.course": "Math", "Grade.score": {"$GT": 60}}), it should be possible, but actually not, so

If you check it, you can query the above record. As follows:

But in fact, it should not be queried. Why is this? Because {"Grade.course": "Math", "Grade.score": {"$GT": 60}} is not a whole, it may

will be matched separately. That might course match math, but score matches 80 or 75, so the data comes out.

So what should we do? As shown below:

Db.stu.find ({"Grade": {"$elemMatch": {"course": "Math", "score": {"$GT": 60}}}).

In other words, the $elementMatch is to bind the condition to a whole, and then match.

Description: "$elemMatch" groups the qualifying conditions, which are only used when multiple key operations are required on an inline document.

(8) "$where" query: This is a universal query, because it is our own code to control. It is also easier to write, which is the JS code. Examples are as follows:

Query the record of the user named Zhangsan: as follows:

It can be understood that $where is also a special key, but its value is a function (this notation is in accordance with the JS syntax format). And do what we want to do in the function.

Things. How is it implemented? My understanding is that every document that is queried will execute this function. This in the function represents the docum that is currently being matched

Ent object, which is then checked through the properties in the object, returns True if it meets the requirements, or False if it returns true, indicating that the record

is a meet the requirements. As for this example, it is certainly easy to do so with other queries. It's easy to demonstrate and easy to do. $where Query method is suitable for complex

of the query. It's very powerful, but it's not recommended, it's going to sacrifice performance, but I don't know about that. As for what to add, think about it later.

(9) Limit () function: How many records to take. For example: Db.customer.find (). Limit (5), which is to fetch the previous 5 records

(Ten) Skip () function: How many records are skipped. For example: Db.customer.find (). Skip (5): Skips 5 records, starting with 6 records.

Description: db.collections name. Find (). Skip (N): Skips n records, starting with the n+1 record.

(one) sort () function: sort. Syntax: db.collections name. Find (). sort (the field to sort by: 1), where 1 indicates ascending, and if 1 is descending.

You can also specify more than one sort of field in sort such as: Db.persons.find (). Sort ({"Age": 1, "_id":-1}): That is, first, by the ascending

Follow _id Descending.

(12) Cursor: This is easier to understand, db.persons.find () is a cursor that returns a pointer to the result set. For example, we can manipulate data like this:

var p = db.persons.find ()

while (P.hasnext ()) {
var o = Persons.next ();
Printjson (o);
}

The results are as follows:

Note: Several destruction conditions for Cursors 1. The client sent him a message to destroy 2. Cursor iteration completed 3. The default cursor is more than 10 minutes and will not be cleared.

MongoDB (v): MongoDB additions and deletions (4)----query details (below)

Related Article

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.