Get the App
SLTechnology News&Howtos  ›  Database  › 

Detailed explanation of MongoDB query Operation

Shulou Source: shulou.com Published: 2022-06-01 02:19:40 09月17日 Update

Detailed explanation of MongoDB query Operation

Query using find or findOne. And can carry out range query, data set query, inequality query, and other queries.

The query will return DBcursor cursors only if you need them.

Operate on the document (result set) returned by the cursor, for example, ignore a certain number of results, or return the number of results, and sort the results.

1. Specify the key to be returned

Sometimes only interested in a few key values of the document, you can shield the returned uninterested key values and return the interested key values.

Mongos > db.blog.find ({}, {"name": 1}) {"_ id": ObjectId ("5659836e73c8340448fa470e")} {"_ id": ObjectId ("565e5c0e73c8340448fa470f")} {"_ id": ObjectId ("565f998e73c8340448fa4710")} {"_ id": 1, "name": "zmkzmkzmkzmkzmk"} {"_ id": 2, "name": "zmkzmkzmkzmkzmk"} {"_ id": 3 "name": "zmkzmkzmkzmkzmk"} {"_ id": ObjectId ("566004d173c8340448fa4712"), "name": "zmk"}

You can do this:

Mongos > db.blog.find ({}, {"name": 1, "_ id": 0}) {"name": "zmkzmkzmkzmkzmk"} {"name": "zmk"}

Specify the key value returned so that the query result is more efficient. This API can be used for conditional query to facilitate result processing.

two。 Query condition

Comparison operator

"$lt", "$lte", "$gt", and "$gte" correspond to = respectively.

Tip: define the approximate format of the method in mongodb shell:

Mongos > var insertName=function () {… For (var iTuno insertName ()

Effect:

Mongos > db.blog.find () {"_ id": 1, "name": "zmk0"} {"_ id": 0, "name": "zmk0"} {"_ id": 2, "name": "zmk2"} {"_ id": 3, "name": "zmk3"} {"_ id": 4 "name": "zmk4"} {"_ id": 5, "name": "zmk5"} {"_ id": 6, "name": "zmk6"} {"_ id": 7, "name": "zmk7"} {"_ id": 8, "name": "zmk8"} {"_ id": 9, "name": "zmk9"}

Observing the following effect, find can use "$lt" < to query the time the file was inserted

Mongos > db.blog.insert ({"_ id": 10, "time": new Date ()}) WriteResult ({"nInserted": 1}) mongos > db.blog.find ({"time": {"$lt": new Date ()}}) {"_ id": 10, "time": ISODate ("2015-12-05T12:08:53.469Z")}

3. Query array

1.$all

Applies to multiple element matching arrays and is not in order.

Mongos > db.blog.insert ({"_ id": 1, "fruit": ["apple", "banana", "peach"]}) WriteResult ({"nInserted": 1}) mongos > db.blog.insert ({"_ id": 2, "fruit": ["apple", "kumquat", "orange"]}) WriteResult ({"nInserted": 1}) mongos > db.blog.insert ({"_ id": 3, "fruit": ["cherry", "banana") "apple"]}) WriteResult ({"nInserted": 1}) mongos > db.blog.find ({"fruit": {$all: ["apple", "banana"]}) {"_ id": 1, "fruit": ["apple", "banana", "peach"]} {"_ id": 3, "fruit": ["cherry", "banana", "apple"]}

To match the array exactly, you can specify the subscript using key.index syntax

Mongos > db.blog.find ({"fruit.2": "peach"}) {"_ id": 1, "fruit": ["apple", "banana", "peach"]}

2.$size

Query an array of a specific length.

Mongos > db.blog.find ({"fruit": {$size:3}) {"_ id": 1, "fruit": ["apple", "banana", "peach"]} {"_ id": 2, "fruit": ["apple", "kumquat", "orange"]} {"_ id": 3, "fruit": ["cherry", "banana", "apple"]}}

You can add the size key to the document that restores the array, adding elements to the specified array each time, while increasing the value of "size". The self-increment operation is very fast and has no impact on performance.

3.slice

You can return a subset of array elements matched by a key

Mongos > db.blog.find ({"fruit": {size:3}}, {"fruit": {"slice":-2}) {"_ id": 2, "fruit": ["kumquat", "orange"]} {"_ id": 3, "fruit": ["banana", "apple"]}

4. Query embedded documents

Just click the grammar.

Mongos > db.people.find ({"name.first": "Joe", "name.last": "Schmoe"})

To specify a set of elements correctly without specifying each key, you need to use $elemMatch to match a single embedded document in the array in the query condition.

Db.blog.find ({"comments": {"$elemMatch": {"author": "joe", "score": {"$gte": 5})

5.limit, skip and sort

Three methods can be combined for paging.

For example, you have an online store where someone searches for mp3 and wants to return 50 results per page in ascending order from high to low.

Db.stock.find ({"des": "mp3"}) .limit (50). Sort ({"price":-1}) db.stock.find ({"des": "mp3"}) .skip (50) .limit (50). Sort ({"price":-1}).

However, skipping too much can cause performance problems.

Solution: try to avoid using skip to skip a large number of results.

Var page1=db.stock.find ({"des": "mp3"}) .limit (50). Sort ({"price":-1}) var lastest=null;// solution to record the last document of the cursor as part of the condition of the second query while (page1.hasNext ()) {lastest=page1.next (); display (lastest) } var page2=db.stock.find ({"des": "mp3"}, {"price": {$gt:lastest.price}}) .limit (50) .sort ({"price":-1}).

Randomly select documents

The correct way is to insert a key with a random value when inserting the document, and then find () according to the normal query.

Thank you for reading, if you have any questions, please leave a message or go to the community to exchange and discuss, thank you for your support!

Tags: Query document array result method element condition interest cursor performance effect quantity grammar instance ordinary accurate three subscript inequality price Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS NVidia MariaDB Shulou Information