我有两个清单,例如清单和任务。两种模式如下所示
清单结构看起来像beow
{
"_id": "5b7d0f77e231b6b530b0ee5a",
"audit_checklist_type": "Weekly"
},
{
"_id": "5b7d3f33e7a57f38084efb09",
"audit_checklist_type": "Daily"
}
任务架构如下所示
{
"_id": "5b7d65daf74be318e8378cf9",
"checklist_id": "5b7d3f33e7a57f38084efb09"
},
{
"_id": "5b7d662df74be318e8378cfb",
"checklist_id": "5b7d3f33e7a57f38084efb09"
}
我的查询是
AuditChecklist.aggregate([
{ $match: {
$and: [
audit_checklist_type : "Daily"
]
} },
{
$lookup: {
from: 'AuditTask',
localField: '_id',
foreignField: 'checklist_id',
as: 'TaskData'
}
},
]).exec();
我正在尝试获得类似的输出
{
"_id": "5b7d3f33e7a57f38084efb09",
TaskData": [
{
"_id": "5b7d65daf74be318e8378cf9",
"checklist_id": "5b7d3f33e7a57f38084efb09"
},
{
"_id": "5b7d662df74be318e8378cfb",
"checklist_id": "5b7d3f33e7a57f38084efb09"
}
]
}
但是对我而言,结果TaskData输出为空。 任何帮助是极大的赞赏。预先感谢!
答案 0 :(得分:1)
Mongooose使数据库名称复数,所以应该使用AuditTask
而不是使用audittasks
OR
您可以先将数据库导入文件中,例如
import AuditTask from './AuditTask`
or
const AuditTask = require('./AuditTask')
并在您的$lookup
聚合中使用它
{ '$lookup': {
'from': AuditTask.collection.name,
'localField': '_id',
'foreignField': 'checklist_id',
'as': 'TaskData'
}}