Mongodb返回多个子数组结果,并排除其他返回结果

编程入门 行业动态 更新时间:2024-10-24 20:15:27
本文介绍了Mongodb返回多个子数组结果,并排除其他返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有节点数组的以下集合:

I have the following collection with a node array:

{ "_id" : ObjectId("55acf6187d4c31475417fa62"), "node" : [ { "-id" : "29331496", "-uid" : "1899168", "-changeset" : "26313303", "-lat" : "-37.6104102", "-lon" : "144.9459817", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" : "true", "-version" : "6" }, { "-id" : "29331497", "-uid" : "1899168", "-version" : "2", "-lon" : "144.9451088", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" : "true", "-changeset" : "26313303", "-lat" : "-37.601881" }, { "-id" : "29331498", "-timestamp" : "2014-10-25T03:36:51Z", "-version" : "3", "-uid" : "1899168", "-user" : "heyitsstevo", "-visible" : "true", "-changeset" : "26313303", "-lat" : "-37.6011267", "-lon" : "144.9448575" }, { "-lon" : "144.943302", "-id" : "29331499", "-timestamp" : "2011-11-23T03:21:40Z", "-user" : "melb_guy", "-version" : "9", "-uid" : "11111", "-visible" : "true", "-changeset" : "9916439", "-lat" : "-37.5983291" }, { "-id" : "60648717", "-uid" : "46482", "-user" : "Zulu99", "-lat" : "-37.6796337", "-lon" : "144.9220639", "-timestamp" : "2009-12-12T21:29:36Z", "-visible" : "true", "-version" : "2", "-changeset" : "3358816" }, { "-id" : "60648718", "-timestamp" : "2009-12-12T21:29:35Z", "-uid" : "46482", "-version" : "2", "-changeset" : "3358816", "-user" : "Zulu99", "-visible" : "true", "-lat" : "-37.6787103", "-lon" : "144.9224609" }, { "-id" : "60648719", "-timestamp" : "2009-12-12T21:28:58Z", "-user" : "Leon K", "-version" : "2", "-changeset" : "3358816", "-uid" : "Zulu99", "-visible" : "true", "-lat" : "-37.677841", "-lon" : "144.9227344" } ] }

如何返回所有具有"-user" ="Zulu99"且不包含其他内容的节点?

How do I return all nodes that have the "-user" = "Zulu99" and exclude anything else?

我尝试了以下查询,但是它只返回它找到的带有"Zulu99"的第一个节点:

I have tried the following query but it only returns the first node it finds with "Zulu99":

db.osm.find( { }, { node: {$elemMatch: {'-user': 'Zulu99'}}} )

推荐答案

如果文档尺寸更大,则aggregation $unwind创建 笛卡尔积 问题,即每个数组对象都会创建多个文档,因此会创建慢速聚合查询.如果要避免此问题,请使用 $ redact 如下:

If your documents size more then aggregation $unwind creates Cartesian Product problem i.e every array object creates multiple documents so it creates slow down aggregation query, If you want avoid this problem use $redact as like below :

db.collectionName.aggregate({ "$match": { "node.-user": "Zulu99" } }, { "$redact": { "$cond": { "if": { "$eq": [{ "$ifNull": ["$-user", "Zulu99"] }, "Zulu99"] }, "then": "$$DESCEND", "else": "$$PRUNE" } } }).pretty()

编辑

如果要同时满足多个条件,则可以将$cond与$and一起使用

If you want to multiple conditions then used $cond with $and like this

db.collectionName.aggregate({ "$match": { "node.-user": "Zulu99", "node.-lat": "-37.6787103" } }, { "$redact": { "$cond": { "if": { "$and": [{ "$eq": [{ "$ifNull": ["$-user", "Zulu99"] }, "Zulu99"] }, { "$eq": [{ "$ifNull": ["$-lat", "-37.6787103"] }, "-37.6787103"] }] }, "then": "$$DESCEND", "else": "$$PRUNE" } } }).pretty()

在上面的查询中,您得到lat检查是否等于-37.6787103,如果要检查-lat and -lon至$gt or $lt,则首先应在您的计算机中更改两个lat and lon的数据类型.文档看起来像String,因此首先更改数据类型String to number,然后使用比较运算符.

In above query you get that lat check whether is equal to -37.6787103 or not, if you want to check -lat and -lon to $gt or $lt then first you should changed your data type of both lat and lon in your documents it looks like String so first changed data type String to number then used comparison operator.

第二件事,如果只想匹配node数组对象,则在readact之后使用group,像这样:

Second things if you want only matched node array object then used group after readact like this :

db.collectionName.aggregate({ "$match": { "node.-user": "Zulu99", "node.-lat": "-37.6787103" } }, { "$redact": { "$cond": { "if": { "$and": [{ "$eq": [{ "$ifNull": ["$-user", "Zulu99"] }, "Zulu99"] }, { "$eq": [{ "$ifNull": ["$-lat", "-37.6787103"] }, "-37.6787103"] }] }, "then": "$$DESCEND", "else": "$$PRUNE" } } }, { "$group": { "_id": "$_id", "node": { "$first": "$node" // use $first } } }).pretty()

新编辑

如果要查找gte and lte条件,请遵循以下聚合:

If you want to find out gte and lte condition then follow this aggregation :

db.collectionName.aggregate({ "$project": { "check": { "$setDifference": [{ "$map": { "input": "$node", "as": "node1", "in": { "$cond": { "if": { "$and": [{ "$and": [{ "$gte": ["$$node1.-lon", 100] }, { "$lte": ["$$node1.-lon", 150] }] }, { "$and": [{ "$gte": ["$$node1.-lat", -50] }, { "$lte": ["$$node1.-lat", -10] }] }] }, "then": "$$node1", "else": false } } } }, [false] ] } } }).pretty()

更多推荐

Mongodb返回多个子数组结果,并排除其他返回结果

本文发布于:2023-10-18 13:20:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数组   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!