带管道的Mongo-Scala驱动程序聚合查找

时间:2018-10-15 20:58:43

标签: mongodb-scala

我希望通过mongo-scala-driver版本2.30使用管道使用新的查找功能来实现聚合。

我要实现的MongoQuery聚合阶段是:

$lookup: {
    from: "user",
    let: { recipeId: "$_id" },
    pipeline:[
        { $match:
            { $expr: 
                { $in: ["$$recipeId", "$licenses.recipe"] }
            }                
        }
    ],
    as: "users"
}

我当前的工作阶段是:

val let = Seq(Variable("recipeId", "$_id"))    
val theMatch = Document.apply("""{$match:{$expr:{ $in: ["$$recipeId", "$licenses.recipe"] }}}""").toBsonDocument
val pipeline = Seq(theMatch)
val theLookup = lookup("user", let, pipeline, "users")

val aggr = Seq(
  ...,
  theLookup)

这确实可行,但我很确定有更好的方法来使用提供的驱动程序库并更好地实现我以前使用的Document.apply brute force方法,例如, theMatch

建议表示赞赏。

0 个答案:

没有答案
相关问题