【发布时间】:2021-02-08 00:53:08
【问题描述】:
我有两个集合Projects 和Users。
Projects 的预算字段包含以下内容:1)amount 和 2)currency
{
_id: ObjectId(...),
type: 'typeA',
budget: {
amount: 123,
currency: 'USD'
}
}
Users 有一个名为bids 的字段,其中包含具有amount 和currency 字段的对象列表。
{
_id: ObjectId(...),
name: "User name",
bids: [{amount: 123, currency: "USD"}, {amount: 342, currency: "INR"}]
}
我正在尝试使用查找聚合将Users 与Projects 连接起来。
db.Projects.aggregate([
{
$lookup: {
from: "Users",
let: { projectAmount: "$budget.amount", projectCurrency: "$budget.currency" },
pipeline: [
{$match: {
$expr: {
$and: [
{ $eq: ["$bids.amount", "$$projectAmount"] },
{ $eq: ["$bids.currency", "$$projectCurrency"] }
]
}
}}
],
as: "matchingBids"
}
]);
但我总是得到空结果,尽管我在用户集合中有一些匹配的对象。我浏览了官方文档和互联网,但没有发现任何帮助。任何帮助,将不胜感激。谢谢
【问题讨论】:
-
请在您的问题中也添加预期的输出。这将有助于修复查询
标签: node.js mongodb mongoose aggregation-framework