在所需的管道阶段不可用时,如何使用Reactive MongoDB Aggregation Framework? 我对lookup stage with pipeline感兴趣,但是在文档中我只找到
reactivemongo.api.commands.AggregationFramework.Lookup.apply(from: String, localField: String, foreignField: String, as: String)
最初的代码等同于MongoDB JS代码
db.tournament.aggregate([
{ $match : { "tid" : "abacaba"} },
{ $group : { _id : "$t" , "m" : {
{ $sort : { "m" : -1 } },
$push : {"u" : "$uid", "m" : "$m" } }
}},
{ $project : { "p" : { $slice : ["$m", 20]} } }
]);
此代码的性能问题是我们将所有内容推入数组,仅在投影阶段丢弃不需要的数据。因此,在阅读StackOverflow answer之后, 我将其重写为
{ $match : { "tid" : "abacaba"} },
{ $sort : { "m" : -1 } },
{ $group : { "_id" : "$t"} },
{ $lookup : {
"from" : "tournament",
"let" : { "i" : "$_id" },
"pipeline" : [
{ $match : { "$expr" : { "$eq" : [ "$t", "$$i"] } } },
{ $sort : { "m" : -1 } },
{ $limit : 20 },
{ $project : { "uid" : 1, "m" : 1} }
],
"as" : "p"
}}
]);
但是我坚持将其翻译为Reactive MongoDB Scala代码。
UPD1。我意外发现piece of code具有必要的查询语法。并提出了另一个问题。在我的情况下,Sharded Collection Restrictions的外观如何,当我在其上运行aggregate()方法的集合等于来自集合的时候?