我有两个集合,具有以下文档结构:评论集:
{
"_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")
}
]
}
现在,"来自"集合文档被添加为类型数组的字段名称下的对象。如果我使用$放开数数,则将其作为对象给出。我不想成为对象,而是希望最终文档在加入后具有以下结构:$查找中的匹配条件的字段&应该将常见的列组合在一起,以避免重复字段。新字段将添加到新文档中。例如:
{
"_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
运算符,该操作员将在即将发布的3.6版本中。
$mergeObject
将字段与加入的收集字段合并为$replaceRoot
,以将组合的DOC提升到最高级别。
$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"
}
])