比较同一文档中的两个数组字段

时间:2018-09-29 08:44:40

标签: mongodb mongodb-query aggregation-framework

我有两个带有数据的不同数组,我必须将两个对象的同一对象合并到一个对象中,如何在MongoDB Aggregation的帮助下进行合并。 这是我的两个数组

{"ids" : [
    {
        "_id" : ObjectId("5ba8d8dfaa988532967029af"),
        "level" : 2,
        "completed" : 5,
        "asset" : ObjectId("5ba8caa1aa98853296702989")
    },
    {
        "_id" : ObjectId("5ba8d8dfaa988532967029b0"),
        "level" : 2,
        "completed" : 3,
        "asset" : ObjectId("5ba8caf6aa9885329670298a")
    },
    {
        "_id" : ObjectId("5ba8d8dfaa988532967029b1"),
        "level" : 2,
        "asset" : ObjectId("5ba8cb09aa9885329670298b")
    }]}

{"total" : [
    {
        "total" : 1,
        "asset" : ObjectId("5ba8caa1aa98853296702989"),
        "level" : 2
    },
    {
        "total" : 1,
        "asset" : ObjectId("5ba8caf6aa9885329670298a"),
        "level" : 2
    },
    {
        "total" : 1,
        "asset" : ObjectId("5ba8cb09aa9885329670298b"),
        "level" : 2
    }]}

在上面两个数组中显示了数据,我只想合并资产和级别相同的那个对象

Expected Result:[{
                 "level" : 2,
                 "asset" : ObjectId("5ba8caa1aa98853296702989"),
                 "total" : 1,
                 "completed" : 5,
                 },{
                 "level" : 2,
                 "asset" : ObjectId("5ba8caf6aa9885329670298a"),
                 "total" : 1,
                 "completed" : 3,
                }]

2 个答案:

答案 0 :(得分:2)

如果您已经有两个对象,则可以这样做。但是如果您需要从mongoDb中获取它,则应该在mongo聚合中使用$ mergeObject。

var a = [
    {
        "_id" : "5ba8d8dfaa988532967029af",
        "level" : 2,
        "completed" : 5,
        "asset" : "5ba8caa1aa98853296702989"
    },
    {
        "_id" : "5ba8d8dfaa988532967029b0",
        "level" : 2,
        "completed" : 3,
        "asset" : "5ba8caf6aa9885329670298a"
    }];
    
 var b = [
    {
        "total" : 1,
        "asset" : "5ba8caa1aa98853296702989",
        "level" : 2
    },
    {
        "total" : 1,
        "asset" : "5ba8caf6aa9885329670298a",
        "level" : 2
    }];
    var output = [];
    function extend(obj, src) {
	src.forEach(function(key,index) { 
          if(obj[index]["asset"] == src[index]["asset"] && obj[index]["level"] == src[index]["level"]){
            let c = {...obj[index],...src[index]};
            output.push(c);
           }
         
        });
    }
  extend(a, b);
  console.log(output)

答案 1 :(得分:0)

您可以尝试以下汇总

db.collection.aggregate([
  { "$project": {
    "Result": {
      "$map": {
        "input": "$total",
        "as": "c",
        "in": {
          "total": "$$c.total",
          "level": "$$c.level",
          "asset": "$$c.asset",
          "completed": {
            "$arrayElemAt": [
              "$ids.completed",
              { "$indexOfArray": ["$ids.asset", "$$c.asset"] }
            ]
          }
        }
      }
    }
  }}
])

Output

[
  {
    "Result": [
      {
        "asset": ObjectId("5ba8caa1aa98853296702989"),
        "completed": 5,
        "level": 2,
        "total": 1
      },
      {
        "asset": ObjectId("5ba8caf6aa9885329670298a"),
        "completed": 3,
        "level": 2,
        "total": 1
      },
      {
        "asset": ObjectId("5ba8cb09aa9885329670298b"),
        "level": 2,
        "total": 1
      }
    ]
  }
]