【问题标题】:How to merge the collections to get the same document schema?如何合并集合以获得相同的文档架构?
【发布时间】:2018-02-23 19:51:23
【问题描述】:

我有两个具有以下文档结构的集合: 评论收集:

{ 
    "_id" : ObjectId("59bab6c6d41dce6422af08cd"), 
    "userId" : 12345.0, 
    "comment" : "Hey, what's up?", 
    "created" : ISODate("2017-09-14T17:05:10.820+0000")
}
{ 
    "_id" : ObjectId("59bab6c6d41dce6422af08ce"), 
    "userId" : 123456.0, 
    "comment" : "Not much", 
    "created" : ISODate("2017-09-14T17:05:10.855+0000")
}
{ 
    "_id" : ObjectId("59bab6c6d41dce6422af08cf"), 
    "userId" : 12345678.0, 
    "comment" : "Cool", 
    "created" : ISODate("2017-09-14T17:05:10.889+0000")
}
{ 
    "_id" : ObjectId("59bab6c6d41dce6422af08d0"), 
    "userId" : 12.0, 
    "comment" : "Nothing", 
    "created" : ISODate("2017-09-14T17:05:10.931+0000")
}

用户集合:

{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d1"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Rich", 
    "lastName" : "S", 
    "gender" : "M", 
    "country" : "CA", 
    "age" : "18"
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d2"), 
    "unique_Id" : 123456.0, 
    "firstName" : "Rob", 
    "lastName" : "M", 
    "gender" : "M", 
    "country" : "US", 
    "age" : "25"
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d3"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Sarah", 
    "lastName" : "T", 
    "gender" : "F", 
    "country" : "US", 
    "age" : "13"
}

我尝试加入他们,并且需要他们在加入后遵循相同的文档架构。 我做了

db.getCollection('users').aggregate([
    {
        $lookup: {
            from: "comments",
            localField: "unique_Id",
            foreignField: "userId",
            as: "goldStandard"
        }

    },
    { $out : "test2"}
])

输出:

{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d1"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Rich", 
    "lastName" : "S", 
    "gender" : "M", 
    "country" : "CA", 
    "age" : "18", 
    "goldStandard" : [
        {
            "_id" : ObjectId("59bab6c6d41dce6422af08cd"), 
            "userId" : 12345.0, 
            "comment" : "Hey, what's up?", 
            "created" : ISODate("2017-09-14T17:05:10.820+0000")
        }
    ]
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d2"), 
    "unique_Id" : 123456.0, 
    "firstName" : "Rob", 
    "lastName" : "M", 
    "gender" : "M", 
    "country" : "US", 
    "age" : "25", 
    "goldStandard" : [
        {
            "_id" : ObjectId("59bab6c6d41dce6422af08ce"), 
            "userId" : 123456.0, 
            "comment" : "Not much", 
            "created" : ISODate("2017-09-14T17:05:10.855+0000")
        }
    ]
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d3"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Sarah", 
    "lastName" : "T", 
    "gender" : "F", 
    "country" : "US", 
    "age" : "13", 
    "goldStandard" : [
        {
            "_id" : ObjectId("59bab6c6d41dce6422af08cd"), 
            "userId" : 12345.0, 
            "comment" : "Hey, what's up?", 
            "created" : ISODate("2017-09-14T17:05:10.820+0000")
        }
    ]
}

现在,“from”集合文档作为对象添加到数组类型的“as”字段名称下。如果我使用 $unwind 来展开数组,那么它将作为对象给出。我不想成为一个对象,相反,我希望最终的文档在加入后具有以下结构: $lookup & common 列中匹配条件的字段要合并在一起,避免重复字段。新字段将添加到新文档中。例如:

{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d1"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Rich", 
    "lastName" : "S", 
    "gender" : "M", 
    "country" : "CA", 
    "age" : "18",  
    "comment" : "Hey, what's up?", 
    "created" : ISODate("2017-09-14T17:05:10.820+0000")
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d2"), 
    "unique_Id" : 123456.0, 
    "firstName" : "Rob", 
    "lastName" : "M", 
    "gender" : "M", 
    "country" : "US", 
    "age" : "25", 
    "comment" : "Not much", 
    "created" : ISODate("2017-09-14T17:05:10.855+0000")
}
{ 
    "_id" : ObjectId("59bab74cd41dce6422af08d3"), 
    "unique_Id" : 12345.0, 
    "firstName" : "Sarah", 
    "lastName" : "T", 
    "gender" : "F", 
    "country" : "US", 
    "age" : "13", 
    "comment" : "Hey, what's up?", 
    "created" : ISODate("2017-09-14T17:05:10.820+0000")
}

请提出建议。

【问题讨论】:

  • 可以在即将发布的 3.6 版本的 3.5 开发版本中使用聚合运算符 $mergeObject。不确定这是否适合您。

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

您可以使用$mergeObject 运算符,该运算符将在即将发布的 3.6 版本中提供。

$mergeObject 将字段与加入的集合字段合并,然后$replaceRoot 将合并的文档提升到顶层。

$project 排除删除 goldStandard 字段和 $out 写入新集合。

类似

db.getCollection('users').aggregate([
  {
    "$lookup": {
      "from": "comments",
      "localField": "unique_Id",
      "foreignField": "userId",
      "as": "goldStandard"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$mergeObjects": [
          "$$ROOT",
          {
            "$arrayElemAt": [
              "$goldStandard",
              0
            ]
          }
        ]
      }
    }
  },
  {
    "$project": {
      "goldStandard": 0
    }
  },
  {
    "$out": "test2"
  }
])

【讨论】:

  • 我检查了它的实现,但是现在如何使用这个未来的功能呢?我有 3.4.6 版,其中运行 $mergeObjects 会出现以下错误:无法识别的表达式 '$mergeObjects''
  • 它在 3.5 开发系列中可用,您可以从 here 下载更多信息 here
猜你喜欢
  • 2015-04-30
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
相关资源
最近更新 更多