我需要使用PyMongo驱动程序通过无序的不同字段对(sender
和recipient
)对特定集合中的记录进行分组。
例如,对(sender_field_value,recipient_field_value)和(recipient_field_value,sender_field_value)被认为是相等的。
我的汇总管道
groups = base.flow.records.aggregate([
{'$match': {'$or': [
{'sender': _id},
{'recipient': _id}
]
}
},
{'$group': {
'_id': {
'sender': '$sender',
'recipient': '$recipient',
},
'data_id': {
'$max': '$_id'
}
}
},
{'$limit': 20}
])
应用于数据
{ "_id" : ObjectId("533950ca9c3b6222569520c2"), "recipient" : ObjectId("533950ca9c3b6222569520c1"), "sender" : ObjectId("533950ca9c3b6222569520c0") }
{ "_id" : ObjectId("533950ca9c3b6222569520c4"), "recipient" : ObjectId("533950ca9c3b6222569520c0"), "sender" : ObjectId("533950ca9c3b6222569520c1") }
产生以下
{'ok': 1.0,
'result': [
{'_id': {'recipient': ObjectId('533950ca9c3b6222569520c0'), 'sender': ObjectId('533950ca9c3b6222569520c1')},
'data_id': ObjectId('533950ca9c3b6222569520c4')},
{'_id': {'recipient': ObjectId('533950ca9c3b6222569520c1'), 'sender': ObjectId('533950ca9c3b6222569520c0')},
'data_id': ObjectId('533950ca9c3b6222569520c2')}
]
}
但所需的结果只是
{'ok': 1.0,
'result': [
{'_id': {'recipient': ObjectId('533950ca9c3b6222569520c0'), 'sender': ObjectId('533950ca9c3b6222569520c1')},
'data_id': ObjectId('533950ca9c3b6222569520c4')}
]
}
什么是合适的管道?
答案 0 :(得分:2)
实现独特配对分组的技巧是将$ group _id传递给同一个'无论哪种情况。我会使用正常的比较来做到这一点(你可以提出一些更适合你的情况的东西 - 如果你的发件人和收件人不能直接比较我的解决方案不起作用):
{$project : {
"_id" : 1,
"groupId" : {"$cond" : [{"$gt" : ['$sender', '$recipient']}, {big : "$sender", small : "$recipient"}, {big : "$recipient", small : "$sender"}]}
}},
{$group: {
'_id': "$groupId",
'data_id': {
'$max': '$_id'
}
}}
完整的聚合管道如下所示:
{$match : {
'$or': [{'sender': userId},{'recipient': userId}]
}},
{$project : {
"_id" : 1,
"groupId" : {"$cond" : [{"$gt" : ['$sender', '$recipient']}, {big : "$sender", small : "$recipient"}, {big : "$recipient", small : "$sender"}]}
}},
{$group: {
'_id': "$groupId",
'data_id': {
'$max': '$_id'
}
}},
{$limit: 20}