Doctrine ODM:使用聚合构建器在聚合字段上创建 $lookup
Doctrine ODM: create $lookup on aggregated field with aggregation builder
在简化的数据模型中,我有三种类型的文档:项目、用户和分配。用户和项目存储在它们自己的集合中,而分配则嵌入在项目中。示例项目可能如下所示:
{
"_id" : ObjectId("xxx"),
"name" : "yyy",
"assignments" : [
{
"assignmentDate" : ISODate("2018-01-11T10:05:20.125Z"),
"user" : ObjectId("zzz"),
},
{
"assignmentDate" : ISODate("2018-01-12T10:05:20.125Z"),
"user" : ObjectId("iii"),
}
]
}
我想查询当前分配给给定用户的所有项目。这个聚合管道完成了这项工作:
db.Item.aggregate([
{
$addFields: {
currentAssignment: { $arrayElemAt: ['$assignments', -1] }
}
},
{
$lookup: {
from: 'User',
localField: 'currentAssignment.user',
foreignField: '_id',
as: 'currentUser'
}
},
{
$match: {
'currentUser.name': { $eq: 'admin' }
}
}
]);
我如何使用 Doctrine ODM Aggregation Builder 构建它? Stage::lookup
方法只接受一个 from
参数。如果我在聚合管道的计算字段上使用它(在本例中为 currentAssignment
),结果为:
arguments to $lookup must be strings, localField: null is type null
也欢迎用于检索所描述数据集的其他解决方案(如果可能,甚至没有聚合?)。
Lookup
阶段有更多方法,其中一种是localField
,它在聚合阶段设置localField
。
在简化的数据模型中,我有三种类型的文档:项目、用户和分配。用户和项目存储在它们自己的集合中,而分配则嵌入在项目中。示例项目可能如下所示:
{
"_id" : ObjectId("xxx"),
"name" : "yyy",
"assignments" : [
{
"assignmentDate" : ISODate("2018-01-11T10:05:20.125Z"),
"user" : ObjectId("zzz"),
},
{
"assignmentDate" : ISODate("2018-01-12T10:05:20.125Z"),
"user" : ObjectId("iii"),
}
]
}
我想查询当前分配给给定用户的所有项目。这个聚合管道完成了这项工作:
db.Item.aggregate([
{
$addFields: {
currentAssignment: { $arrayElemAt: ['$assignments', -1] }
}
},
{
$lookup: {
from: 'User',
localField: 'currentAssignment.user',
foreignField: '_id',
as: 'currentUser'
}
},
{
$match: {
'currentUser.name': { $eq: 'admin' }
}
}
]);
我如何使用 Doctrine ODM Aggregation Builder 构建它? Stage::lookup
方法只接受一个 from
参数。如果我在聚合管道的计算字段上使用它(在本例中为 currentAssignment
),结果为:
arguments to $lookup must be strings, localField: null is type null
也欢迎用于检索所描述数据集的其他解决方案(如果可能,甚至没有聚合?)。
Lookup
阶段有更多方法,其中一种是localField
,它在聚合阶段设置localField
。