mongo-scala driver aggregation lookup with pipeline

216 views
Skip to first unread message

Colin Bester

unread,
Oct 15, 2018, 5:41:35 PM10/15/18
to mongodb-user

I am looking to implement aggregation using new lookup feature using pipeline with mongo-scala-driver version 2.30.


MongoQuery aggregation stage I am trying to implement is:


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


My current working stage is:


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")

//go and run aggregation
val aggr
= Seq(
 
...,
  theLookup
)


While this does work, I am pretty sure there is better way to use supplied driver library and better implement the Document.apply brute force method I used for, as example, theMatch.


Suggestions appreciated.

Wan Bachtiar

unread,
Oct 23, 2018, 7:56:51 PM10/23/18
to mongodb-user

I am pretty sure there is better way to use supplied driver library and better implement the Document.apply brute force method I used for, as example, theMatch

Hi Colin,

Currently there’s an open ticket to add support for full aggregation expression language builder JAVA-1949 (MongoDB Scala driver implementation is built upon the MongoDB Java driver). Please feel free to upvote/watch the ticket to receive notifications update on the ticket. 

In the meantime, building the $expr via Document/BsonDocument is the recommended way.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages