【发布时间】:2018-12-29 04:43:49
【问题描述】:
我在网上搜索过,但找不到任何符合我的情况。情况如下。
我正在使用聚合将一个集合和一个来自另一个集合的文档组合在一起
restaurants.aggregate([
{
$match: {
_id: {
$in: idList
}
}
},
{
$lookup: {
from: "tags",
localField: "details.restaurantType",
foreignField: "details.restaurantType._id",
as: "types"
}
},
{
$project: {
restaurantName: "$details.restaurantName",
restaurantType: "$details.restaurantType",
type: {
$filter: {
input: "$types",
as: "type",
cond: {
$eq: ["$$type._id", "$details.restaurantType"]
}
}
},
currency: "$details.currency",
costPerPax: "$details.costPerPax"
}
}
]);
当前结果
我当前结果中的“类型”字段是 [],我需要一个匹配的值
[
{
"id": "5c20c7a0036dda80a8baabcc",
"restaurantName": "Villagio Restaurant Sutera Mall",
"type": [],
"currency": "RM",
"costPerPax": 22,
},
{
"id": "5c20ceb07715216d3c217b7a",
"restaurantName": "Thai Food Thai Now Sutera Mall",
"type": [],
"currency": "RM",
"costPerPax": 16,
}
]
预期结果
我需要“类型”字段与来自另一个集合的匹配标记名称,例如这样
[
{
"id": "5c20c7a0036dda80a8baabcc",
"restaurantName": "Villagio Restaurant Sutera Mall",
"type": "Western",
"currency": "RM",
"costPerPax": 22,
},
{
"id": "5c20ceb07715216d3c217b7a",
"restaurantName": "Thai Food Thai Now Sutera Mall",
"type": "Thai",
"currency": "RM",
"costPerPax": 16,
}
]
额外信息
餐厅收藏的两份文件
{
"details": {
"restaurantName": "Villagio Restaurant Sutera Mall",
"restaurantType": "5c01fb57497a896d50f498a8"
},
"_id": "5c20c7a0036dda80a8baabcc",
"status": "OP",
"__v": 0
},
{
"details": {
"restaurantName": "Kingshahi Japanese Shop",
"restaurantType": "5c01fb57497a896d50f49879"
},
"_id": "5c20cb4fb7e75180480690c2",
"status": "OP",
"__v": 0
}
标签集合中的一个文档
{
"_id": "5c01fb57497a896d50f49876",
"details": {
"restaurantTypeId": "5c01fb57497a896d50f49877",
"restaurantTypes": [
{
"_id": "5c01fb57497a896d50f49879",
"name": "Asian",
"counter": 1
},
{
"_id": "5c01fb57497a896d50f4987a",
"name": "Bakery",
"counter": 0
}
]
}
}
【问题讨论】:
-
您的
foreignField值不正确。应该是foreignField: "details.restaurantTypes._id", -
哇,谢谢,现在我设法从数据库中获取了一些结果,但是我在每个返回对象的“类型”字段中获得了整个标签文档,我尝试了过滤器,但它给了我 []在结果中。您在我的 $filter 查询中看到任何错误吗?
-
您的 foreignField 是一个数组,这就是您获取整个标签元素的原因。顺便说一句,您使用的是什么版本的 mongo?
-
我使用的是数据库版本v4.0.4
-
我想我需要更多时间才能从数据库中获取结果。我会在 10 分钟后回来
标签: node.js mongodb mongodb-query aggregation-framework