Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按子文档分组并获得值字段的唯一值?_Javascript_Node.js_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Javascript 如何按子文档分组并获得值字段的唯一值?

Javascript 如何按子文档分组并获得值字段的唯一值?,javascript,node.js,mongodb,mongoose,aggregation-framework,Javascript,Node.js,Mongodb,Mongoose,Aggregation Framework,这是我的数据库集合: {"productId" : 1, "isVariant": 1, "isComplete" : 1, "variantId" : 1, "attributeSet" : [ { "name" : "Capacity", "value" : "

这是我的数据库集合:

{"productId" : 1,
"isVariant": 1,
"isComplete" : 1,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "500 GB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 2,
"isVariant": 1,
"isComplete" : 1,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "1 TB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 3,
"isVariant": 1,
"isComplete" : 0,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "500 GB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "2.5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 4,
"isVariant": 1,
"isComplete" : 0,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "1 TB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "2.5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
}
现在我只想发送
isVariation
不为0的属性的数据。我还想发送每个属性的变量值,其中
isComplete=1
。因此,结果应该是这样的

 result : [{
     "id": 3,
     "name": "Capacity",
     "value": [
         "500 GB",
         "1 TB"
     ]
  }, {
     "id": 4,
     "name": "Form Factor",
     "value": [
         "5 inch"
      ]
}]
由于此文档的
isComplete
为0,因此上述结果的值不为2.5英寸。有人能帮我查询一下吗
  • $match
    isComplete
    为1
  • $project
    显示必填字段
  • $unwind
    解构
    属性集
    数组
  • $match
    属性集。isVariation
    不是0
  • $group
    attributeSet.id
    和使用
    $addToSet
  • 在您的查询中不需要$project阶段,我添加了它,因为这将优化您的查询性能


    这回答了你的问题吗?没有t@VishalKumar确保你的问题标题应该有意义,并且简短地回答你的问题,有关详细信息,请参阅[如何提出一个好问题?]()
    db.collection.aggregate([
      { $match: { isComplete: 1 } },
      {
        $project: {
          _id: 0,
          attributeSet: 1
        }
      },
      { $unwind: "$attributeSet" },
      { $match: { "attributeSet.isVariation": { $ne: 0 } } },
      {
        $group: {
          _id: "$attributeSet.id",
          name: { $first: "$attributeSet.name"  },
          value: { $addToSet: "$attributeSet.value" }
        }
      }
    ])