MongoDB:在分片集合上聚合$或匹配项

时间:2018-07-23 06:39:18

标签: mongodb

我有一个以副本集配置运行的分片transactions集合。

我正在尝试汇总特定用户的最新交易列表。该架构本质上是{sender: ObjectId(..), recipient: ObjectId(..), createdAt: .., shardKey: ..}。 shardKey是按字母顺序sender + recipient排序的,因此无论交易方向如何,它都是相同的。

我看到一种行为,其中aggregate$or似乎在两个$or准则中的第一个准则上急切匹配。

示例: 有两条记录: 记录1-{sender: a, recipient: b, createdAt: 2018-09-21} 记录2-{sender: b, recipient: a, createdAt: 2018-09-22}

运行以下查询: db.transactions.aggregate([ { '$match': { '$or': [ { recipient: ObjectId(a) }, { sender: ObjectId(a) } ] } }, { '$project': ... }, { '$group': { sender: { '$last': '$sender' }, recipient: { '$last': '$recipient' } } }, { '$sort': { createdAt: -1 } }], {}) 即使记录1是更新的,也将返回记录2。

将查询调整为仅由发件人查询 db.transactions.aggregate([ { '$match': { '$or': [ { sender: ObjectId(a..) } ] } }, { '$project': ... }, { '$group': { sender: { '$last': '$sender' }, recipient: { '$last': '$recipient' } } }, { '$sort': { createdAt: -1 } }], {}) 将返回记录1。

任何获得此$或执行更像传统SQL OR的方法吗?

0 个答案:

没有答案
相关问题