In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the methods and steps of how to filter the array in the document by mongodb and shares them with you. The details are as follows:
The mongodb document contains an array, and you need to filter out the eligible data in the array and return the result set. You can query group or filter in two ways.
Data source:
{"_ id": ObjectId ("5bbcc0c9a74db9804e78a157"), "uid": "1000001", "name": "zhangsan", "addrs": [{"is_query": "1", "city": "Beijing"}, {"is_query": "0", "city": "Shanghai"} {"is_query": "1", "city": "Shenzhen"}} {"_ id": ObjectId ("5bbcc167a74db9804e78a172"), "uid": "1000002", "name": "lisi", "addrs": [{"is_query": "0", "city": "Beijing"} {"is_query": "0", "city": "Shanghai"}, {"is_query": "1", "city": "Shenzhen"]}
The query is required to specify that under uid, the addrs array contains only the result set with is_query equal to 1 (not included in 0).
Query statement:
Method 1: use $unwind to break up the addrs array, get the result set, filter the eligible data with $match, and finally aggregate the final result set using $group.
Db.getCollection ('user') .aggregate ([{$unwind: "$addrs"}, {$match: {"uid": "1000001", "addrs.is_query": "1"}}, {$group: {"_ id": "$uid" "addrs": {$push: "$addrs"}])
Result:
{"_ id": "1000001", "addrs": [{"is_query": "1", "city": "Beijing"}, {"is_query": "1", "city": "Shenzhen"}]}
Method 2: use $match to filter the eligible root document result set, and then use $project to return the corresponding field, while using $filter in the addrs array to filter internally to return the final result set
Db.getCollection ('user') .aggregate ([{$match: {"uid": "1000001"}}, {$project: {"uid": 1, "name": 1, "addrs": {$filter: {input: "$addrs", as: "item" Cond: {$eq: ["$$item.is_query", "1"]}}])
Result:
{"_ id": ObjectId ("5bbcc0c9a74db9804e78a157"), "uid": "1000001", "name": "zhangsan", "addrs": [{"is_query": "1", "city": "Beijing"}, {"is_query": "1", "city": "Shenzhen"}]}
Compared to the way the $group group aggregate returns the result set, $filter is more elegant and straightforward under the current query requirements. Of course, if you include statistical operations, such as requiring that the number of is_query equal to 1 is returned, then $group is very appropriate.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
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.