Node.js Mongodb 3.6在聚合查找中使用多个条件
我的MongoDb中有两个系列Node.js Mongodb 3.6在聚合查找中使用多个条件,node.js,mongodb,aws-documentdb,aws-documentdb-mongoapi,Node.js,Mongodb,Aws Documentdb,Aws Documentdb Mongoapi,我的MongoDb中有两个系列 度量集合形式 公制 metricCollectionForms文档如下所示 { "_id": ObjectId("5ea25f38afd94f0008d4e6f2"), "approverId": "f08ba2aa-4597-41f0-9e6c-cebf1715ba30", "formName": "Test Form", "formId": "d56209a1-4df0-48de-b6cf-d1ee50200936", } {
metricCollectionForms
文档如下所示
{
"_id": ObjectId("5ea25f38afd94f0008d4e6f2"),
"approverId": "f08ba2aa-4597-41f0-9e6c-cebf1715ba30",
"formName": "Test Form",
"formId": "d56209a1-4df0-48de-b6cf-d1ee50200936",
}
{
"_id": ObjectId("5ea27955bae4900008d996ba"),
"name": "Test Metric",
"type": "INTERNAL",
"formula": [
{
"type": "FORM_FIELD",
"formId": "d56209a1-4df0-48de-b6cf-d1ee50200936",
"formFieldId": "dca2bacf-2cbd-480d-8289-6f3050b635fb"
},
{...}, {...}
],
"formulaLabel": "monthly_production",
"createdBy": "f08ba2aa-4597-41f0-9e6c-cebf1715ba30",
"isApproved": true,
"isActive": false
}
我跳过了上面文档中的几个属性,因为它们与这个问题无关
至于指标
,典型的文档如下所示
{
"_id": ObjectId("5ea25f38afd94f0008d4e6f2"),
"approverId": "f08ba2aa-4597-41f0-9e6c-cebf1715ba30",
"formName": "Test Form",
"formId": "d56209a1-4df0-48de-b6cf-d1ee50200936",
}
{
"_id": ObjectId("5ea27955bae4900008d996ba"),
"name": "Test Metric",
"type": "INTERNAL",
"formula": [
{
"type": "FORM_FIELD",
"formId": "d56209a1-4df0-48de-b6cf-d1ee50200936",
"formFieldId": "dca2bacf-2cbd-480d-8289-6f3050b635fb"
},
{...}, {...}
],
"formulaLabel": "monthly_production",
"createdBy": "f08ba2aa-4597-41f0-9e6c-cebf1715ba30",
"isApproved": true,
"isActive": false
}
您会注意到,metricCollectionForms
中的formId
值正在metric
文档的公式数组formId
字段中引用
因此,一个度量可以在其对象的公式数组中使用多种形式
我试图获取表单列表,并在该列表中返回metricnamewhere的数组
metric.formula.formId=formId(形式)和
metric.isActive=true和
metric.isApproved=真
到目前为止,我的聚合查询如下所示:
{
$lookup: {
from: "metric",
localField: "formId",
foreignField: "formula.formId",
as: "metrics"
}
},
{
$addFields: {
metrics: "$metrics.name"
}
}
现在它确实返回了一个包含所有度量名称的数组,但我不知道如何在$lookup上应用isApproved和isActive true条件
我试着做$pipeline/$match等,但似乎没有任何效果。此外,该解决方案应与Mongo 3.6兼容,因为我在AWS DocumentDb(仅支持3.6)中使用了它。在MongoDB 4.2之前,允许使用该解决方案的管道查找形式不可用
3.6中的最佳选项是在查找之后使用带有表达式的
$addFields
阶段删除数组中不需要的元素。在MongoDB 4.2之前,允许此操作的管道形式的查找不可用
3.6中的最佳选项是在查找之后使用带有表达式的$addFields
阶段从数组中删除不需要的元素