【发布时间】:2020-10-01 04:21:35
【问题描述】:
我在 MongoDb 中有 2 个集合并说:
收藏1
[ {"Name" : "foo1", "Place" : "bar1", "Rank" : 1},
{"Name" : "foo2", "Place" : "bar2", "Rank" : 2},
{"Name" : "foo3", "Place" : "bar3", "Rank" : 3} ]
收藏2
[ {"DocNo" : "One",
"DocArray" : [{"Name" : "ABC", "Number" : 1},
{"Name" : "DEF", "Number" : 2},
{"Name" : "XYZ", "Number" : 4}]},
{"DocNo" : "Two",
"DocArray" : [{"Name" : "JKL", "Number" : 5},
{"Name" : "GHI", "Number" : 1},
{"Name" : "UVW", "Number" : 3}]}
]
我需要在 Collection1 中编写一个查询,其中我有 DocNO 以从 Collection2 获取搜索并仅返回来自 Collection1 的那些文档strong> 其中Rank 字段匹配该特定文档的DocArray 内所有元素的所有Number 字段。
在这个例子中,如果我有DocNo = "Two",那么返回的数组将是这样的:
[ {"Name" : "foo1", "Place" : "bar1", "Rank" : 1},
{"Name" : "foo3", "Place" : "bar3", "Rank" : 3}]
我需要在一个 mongo 查询中完成所有这些操作
【问题讨论】:
-
如果你有一个特定的
DocNO,你为什么要从Collection 1而不是Collection 2开始?
标签: mongodb mongoose mongodb-query aggregation-framework