gpt4 book ai didi

mongodb - 文档数据库 |蒙戈错误: aggregation stage not supported: '$lookup on multiple join conditions and uncorrelated subquery

转载 作者:行者123 更新时间:2023-12-05 04:47:55 25 4
gpt4 key购买 nike

我是 DocumentDB 的新手。尝试加入两个集合并执行查询、排序和投影等过滤器。

我有两个集合,需要对这两个集合执行过滤,结果应该是一个嵌入列表(应该同时包含项目和子项目)。

第一个集合将在 Subitems 字段中保存第二个集合的 ID。

这是第一个集合(Items):

{
"_id": 897979789,
"Country": "India",
"State": "XXX",
"Subitems": [ ObjectId("44497979789"), ObjectId("333897979789") ]
},
{
"_id": 987979798,
"Country": "Australia",
"State": "YYY",
"Subitems": [ ObjectId("97979789444"), ObjectId("897979789333") ]
}

这是第二个集合:

{
"_id": 44497979789,
"EmployeeName": "Krishna",
"Occupation": "Engineer",
"ProjectsHandled": [
{
"ProjectName": "Project1"
}
]
},
{
"_id": 333897979789,
"EmployeeName": "krish",
"Occupation": "CTO",
"ProjectsHandled": [
{
"ProjectName": "Project1"
}
]
},
{
"_id": 97979789444,
"EmployeeName": "name",
"Occupation": "CEO",
"ProjectsHandled": [
{
"ProjectName": "Project2"
}
]
},
{
"_id": 897979789333,
"EmployeeName": "name1",
"Occupation": "manager",
"ProjectsHandled": [
{
"ProjectName": "Project3"
}
]
}

这是我的查询:

let subItemPipeline: any[] = [{ $match: config.subItemsQuery }];
if(Object.keys(config.subItemsSort || {}).length)
subItemPipeline.push({$sort: config.subItemsSort});
if(Object.keys(config.subItemsProjection || {}).length)
subItemPipeline.push({$project: config.subItemsProjection});

let query: any[] = [
{
$match: config.itemsQuery
},
{
$lookup: {
from: "Subitems",
pipeline: subItemPipeline,
as: "Subitems"
}
}
];

if(Object.keys(config.overallQuery || {}).length) query.push({$match: config.overallQuery});
if(Object.keys(config.itemsSort || {}).length) query.push({$sort: config.itemsSort});
if(Object.keys(config.itemsProjection || {}).length) query.push({$project: config.itemsProjection});

const items = await collection.aggregate(query).toArray();

它在 MongoDB 中运行良好,但在 DocumentDB 中遇到以下错误:

Mongoerror: aggregation stage not supported: '$lookup on multiple join conditions and uncorrelated subquery

如有任何帮助,我们将不胜感激

最佳答案

来自 DocumentDB docs :

Amazon DocumentDB supports the ability to do equality matches (for example, left outer join) but does not support uncorrelated subqueries.

蒙古人 docs特别提到这一点:

相等匹配的语法如下:

{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}

这是 DocumentDB 支持的 $lookup 语法,您尝试使用的语法是 3.6 版中添加的"new"$lookup 语法,这就是他们称之为“不相关的子查询”,不幸的是,目前 DocumentDB 不支持它。

最简单的“快速”解决方案是使用默认值向所有 Subitems 文档添加一个新字段。这将允许您使用 equality match 查找。

--- 编辑 ---我建议您为 Subitems 中的每个文档添加一个新字段。

Subitems.updateMany(
{

},
{
$set: {
n: 1
}
}
)

对于您员工的收藏也是如此:

employee.updateMany(
{

},
{
$set: {
n: 1
}
}
)

现在您可以使用equality 查找语法:

{
$lookup: {
from: 'Subitems',
localField: 'n',
foreignField: 'n',
as: 'Subitems',
},
}
... continue to filter sub items.

请注意,这是一个玩具示例,它会在您开始过滤之前逐字查找整个子项集合。

如果您真的想要一个良好的可扩展解决方案,您只需将其分成 2 个不同的调用即可。

关于mongodb - 文档数据库 |蒙戈错误: aggregation stage not supported: '$lookup on multiple join conditions and uncorrelated subquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68283988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com