Joe*_*-IV 2 mongodb mongodb-query aggregation-framework
我希望有人能够回答是否可以使用MongoDB聚合框架来完成我在下面尝试完成的任务.
我有一个类似于以下内容的用户数据结构,有近100万个文档.
{
"firstName" : "John",
"lastName" : "Doe",
"state" : "NJ",
"email" : "JOHNDOE@XYZ.COM"
"source" : [
{
"type" : "SOURCE-A",
"data" : {
"info" : "abc",
"info2" : "xyz"
}
},
{
"type" : "SOURCE-B",
"data" : {
"info3" : "abc"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
为了将数据提供给另一个系统,我需要生成一个平面文件结构,其中包含来自先前数据集的有限信息.列需要表示:
firstname, lastname, email, is_source-a, is_source-b
Run Code Online (Sandbox Code Playgroud)
我遇到困难的部分是试图填充"is_source-a"和"is_source-b"的条件代码.我曾尝试使用以下聚合查询,但无法弄清楚如何使其工作,因为与$ COND一起使用的$ EQ运算符似乎不会评估数组内的数据(总是为false).
db.collection.aggregate([
{
$project : {
_id : 0,
firstName : 1,
lastName: 1,
"is_source-a" : {
$cond : [
{ $eq: [ "$source.type", "source-a" ] },
1,
0
]
},
"is_source-b" : {
$cond : [
{ $eq: [ "$source.type", "source-b" ] },
1,
0
]
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
我可以先对UNWIND数组进行UNWIND,但随后我会为每个用户文档添加多条记录,并且不了解如何将它们合并回来.
在处理对象数组时,是否有一些我缺少如何使用$ EQ(或其他一些运算符)和$ COND?
你肯定是在正确的轨道上,使用它$unwind可以让你在那里,如果你跟着它$group把东西放回原处:
db.collection.aggregate([
{$unwind: '$source'},
{$project: {
_id: 1,
firstName: 1,
lastName: 1,
email: 1,
'is_source-a': {$eq: ['$source.type', 'SOURCE-A']},
'is_source-b': {$eq: ['$source.type', 'SOURCE-B']}
}},
// group the docs that were duplicated in the $unwind back together by _id,
// taking the values for most fields from the $first occurrence of the _id,
// but the $max of the is_source fields so that if its true in any of the
// docs for that _id it will be true in the output for that _id.
{$group: {
_id: '$_id',
firstName: {$first: '$firstName'},
lastName: {$first: '$lastName'},
email: {$first: '$email'},
'is_source-a': {$max: '$is_source-a'},
'is_source-b': {$max: '$is_source-b'}
}},
// project again to remove _id
{$project: {
_id: 0,
firstName: 1,
lastName: 1,
email: 1,
'is_source-a': '$is_source-a',
'is_source-b': '$is_source-b'
}}
])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6234 次 |
| 最近记录: |