In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to query MongoDB documents, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Null
The query for null is slightly different. If I want to query the data where z is null, it looks like this:
Db.sang_collect.find ({z:null})
This will find not only the document where z is null, but also all documents without z field. If you only want to query the field where z is null, add one more condition to determine whether the z field exists, as follows:
Db.sang_collect.find ({z: {$in: [null], $exists:true}}) regular expression query
We have also introduced the use of regular expression queries earlier. The regular expression syntax here is the same as that in JavaScript. For example, querying all documents whose key starts with hello and is not case-sensitive:
Db.sang_collec.find ({xvl / ^ (hello) (. [a-zA-Z0-9]) + / I}) array query
Suppose I have a dataset as follows:
{"_ id": ObjectId ("59f1ad41e26b36b25bc605ae"), "books": [Romance of the three Kingdoms, A Dream of Red Mansions, Water margin]}
Query the documents containing the Romance of the three Kingdoms in books as follows:
Db.sang_collect.find ({books: the Romance of the three Kingdoms})
To query documents with both the Romance of the three Kingdoms and A Dream of Red Mansions, you can use $all as follows:
Db.sang_collect.find ({books: {$all: [Romance of the three Kingdoms, A Dream of Red Mansions]}})
Of course, we can also use exact matching, such as querying the data of books for "Romance of the three Kingdoms", "A Dream of Red Mansions" and "Water margin", as follows:
Db.sang_collect.find ({books: [Romance of the three Kingdoms, A Dream of Red Mansions, Water margin]})
But this will be an exact one-on-one match.
You can also match according to the subscript. For example, I want to query the "Water margin" document of the item with subscript 2 in the array, as follows:
Db.sang_collect.find ({"books.2": water margin})
You can also query by array length, for example, I want to query documents with array length 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 is written here in the position of the second parameter of find. 2 represents the first two elements in the array, and-2 means counting two elements from back to 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: [1p3]}})
The problem of and in the array is also worth mentioning, assuming that I have the following data:
{"_ id": ObjectId ("59f208bc7b00f982986c669c"), "x": [5.0,25.0]}
I want to get the documents in the array whose value value is between (10 and 20), as follows:
Db.sang_collect.find ({x: {$lt:20,$gt:10}})
At this point, although the above document does not meet the criteria, it is still found, because 510, 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 two statements in the query condition with an array element at the same time.
Nested document query
There are two ways to query 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: {ZRV 2pm KRV 3}})
However, this method of writing requires strict matching, and the order can not be changed. If it is written as db.sang_collect.find ({y: {KRV 3je ZR 2}}), it will not match. Therefore, this method is not flexible enough. We generally recommend the following writing method:
Db.sang_collect.find ({"y.z": 2, "y.k": 3})
This way of writing can be reversed at will.
On how to carry out MongoDB document query operation to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.