2017-09-14 61 views
0

我有两个集合与下面的文档结构: 评论集:如何合并集合以获得相同的文档模式?

{ 
    "_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") 
     } 
    ] 
} 

现在,“从”收藏的文件添加为下类型的磁盘阵列“作为”字段名称的对象。如果我使用$ unwind展开数组,则将其作为对象给出。我不希望成为一个对象,相反,我想在加入后最终文档具有以下结构: 具有匹配条件的$ lookup &公共列应该合并在一起以避免重复的字段。新字段将被添加到新文档中。例如:

{ 
    "_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") 
} 

请建议。

+0

有可能使用集成算[$ mergeObject(https://docs.mongodb.com/master/reference/operator/aggregation/mergeObjects/#definition)的3.5版本发展这将是即将发布的3.6版本。不确定这是否是您的选择。 – Veeram

回答

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" 
    } 
]) 
+0

我检查它的实现,但现在如何使用这个未来的功能?我有版本3.4.6,其中运行$ mergeObjects给出以下错误:无法识别的表达式'$ mergeObjects'' – Anubhav

+0

它可在3.5开发系列,你可以从[这里]下载(https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl)更多信息[here](https://docs.mongodb.com/master/release-notes/3.6/) – Veeram