我有两个集合与下面的文档结构: 评论集:如何合并集合以获得相同的文档模式?
{
"_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")
}
请建议。
有可能使用集成算[$ mergeObject(https://docs.mongodb.com/master/reference/operator/aggregation/mergeObjects/#definition)的3.5版本发展这将是即将发布的3.6版本。不确定这是否是您的选择。 – Veeram