我正在从 _id 进行 $lookup。因此结果始终为 1 个文档。
因此,我希望结果是一个对象,而不是一个只有一个项目的数组。
let query = mongoose.model('Discipline').aggregate([
{
$match: {
project: mongoose.Types.ObjectId(req.params.projectId)
},
},
{
$lookup: {
from: "typecategories",
localField: "typeCategory",
foreignField: "_id",
as: "typeCategory"
}
},
{
$project: {
title: 1, typeCategory: "$typeCategory[0]"
}
}
]);
这种表示法:"$typeCategory[0]"
没有起作用。有没有更聪明的方法?
typeCategory:{$arrayElemAt: ["$typeCategory", 0 ]}
。有一个特定的问题需要解决,可以查看这个链接 https://jira.mongodb.org/browse/SERVER-27589。 - s7vr