【发布时间】: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