Mongodb document query operation (II)

Time:2021-12-4

In the previous article, we briefly introduced the query operations in mongodb. In this article, we continue to look at richer query operations.

This is the sixth article in mongodb series. Understanding the previous articles will help you better understand this article:


1.Installing mongodb on Linux
2.Mongodb basic operation
3.Mongodb data type
4.Mongodb document update operation
5.Mongodb document query operation (I)


null

The null query is slightly different. If I want to query the data with Z null, it is as follows:

db.sang_collect.find({z:null})

In this way, not only the documents with null Z will be found, but also all documents without Z field. If you only want to query the field with null Z, add another condition to judge whether the field Z exists, as follows:

db.sang_collect.find({z:{$in:[null],$exists:true}})

Regular expression query

We have also introduced the use of regular expression query earlier. The regular expression syntax here is consistent with that in JavaScript. For example, query all documents whose key is x and value starts with hello and is not case sensitive:

db.sang_collec.find({x:/^(hello)(.[a-zA-Z0-9])+/i})

Array query

Suppose I have a dataset as follows:

{
    "_id" : ObjectId("59f1ad41e26b36b25bc605ae"),
    "books" : [ 
        "Romance of the Three Kingdoms", 
        "Dream of Red Mansions", 
        "Outlaws of the marsh"
    ]
}

Query the documents containing the romance of the Three Kingdoms in books, as follows:

db.sang_ Collect. Find ({books: "Romance of the Three Kingdoms"})

If you want to query documents with both the romance of the Three Kingdoms and the dream of Red Mansions, you can use $all, as follows:

db.sang_ Collect. Find ({books: {$all: ["Romance of the Three Kingdoms", "dream of Red Mansions"]}})

Of course, we can also use exact matching, such as querying books as"Romance of the Three Kingdoms", "dream of Red Mansions", "outlaws of the marsh"The data are as follows:

db.sang_ Collect. Find ({books: ["Romance of the Three Kingdoms", "dream of Red Mansions", "outlaws of the marsh"]})

But this will be a one-to-one exact match.

You can also match by subscript. For example, I want to query the subscript of the item with subscript 2 in the array"Outlaws of the marsh"The following documents are available:

db.sang_ Collect. Find ({"books. 2": "outlaws of the marsh"})

You can also query by array length. For example, I want to query documents with array length of 3:

db.sang_collect.find({books:{$size:3}})

If you want to query the first two pieces of data in the array, you can use $slice as follows:

db.sang_collect.find({},{books:{$slice:2}})

Note that it should be written in the position of the second parameter of find. 2 represents the first two elements in the array, – 2 represents counting two elements from the back to the front. You can also intercept the elements in the middle of the array, such as querying the second to fourth elements of the array:

db.sang_collect.find({},{books:{$slice:[1,3]}})

The problem of and in the array is also worth mentioning. Suppose I have the following data:

{
    "_id" : ObjectId("59f208bc7b00f982986c669c"),
    "x" : [ 
        5.0, 
        25.0
    ]
}

I want to get the document whose value in the array is between (10,20), as follows:

db.sang_collect.find({x:{$lt:20,$gt:10}})

At this time, although the above document does not meet the conditions, it is still found because5<20, and25>10, to solve this problem, we can use $elemmatch as follows:

db.sang_collect.find({x:{$elemMatch:{$lt:20,$gt:10}}})

$elemmatch requires mongodb to compare an array element with two statements in the query criteria at the same time.

Nested document query

There are two query methods for nested documents. For example, my data is as follows:

{
    "_id" : ObjectId("59f20c9b7b00f982986c669f"),
    "x" : 1.0,
    "y" : {
        "z" : 2.0,
        "k" : 3.0
    }
}

To query the above document, my query statement is as follows:

db.sang_collect.find({y:{z:2,k:3}})

But this writing method requires strict matching, and the order cannot be changed. If it is writtendb.sang_collect.find({y:{k:3,z:2}})Therefore, this method is not flexible enough. We generally recommend the following expression:

db.sang_collect.find({"y.z":2,"y.k":3})

This way of writing can reverse the order arbitrarily.

Well, the query operations in mongodb are still very rich. Let’s talk about this first. In the next article, we will introduce cursors. If you have any questions, please leave a message for discussion.

reference material:

1. Authoritative guide to mongodb, 2nd Edition

More information on the official account:
Mongodb document query operation (II)