0

I'm a newbie to MongoDB and Casbah and I'm wondering if anyone can help me please.

I have the following mongoDB Query that works,

db.getCollection('holidayRequests').aggregate
(
   [
     {  $match: {  $and: [ { email: "leeroy.jenkins@company.com" } , { status: "APPROVED" } ] }},
      {
          $group:
      {
           _id: {  email: "$email" },
           totalAmount: { $sum: "$daysTaken" }
      }
     }
   ]
);

How do I convert this to a query the casbah driver will understand?

chridam
  • 100,957
  • 23
  • 236
  • 235
Frankie
  • 21
  • 1

1 Answers1

0

Something like that:

import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.commons.MongoDBList
import com.mongodb.casbah.MongoClient

object testCasbah {

  val mongoClient = MongoClient() /* database connections parameters */
  val db = mongoClient("databaseName")

  val email  =  MongoDBObject("email" -> "leeroy.jenkins@company.com")
  val status =  MongoDBObject("status" ->  "APPROVED" )

  val and    = MongoDBObject("$and" -> List(email, status))

  val pipeline = MongoDBList(MongoDBObject("$match" -> and)) ++
                 MongoDBList(MongoDBObject("$group" -> 
                    MongoDBObject("_id" -> MongoDBObject("email" -> "$email"), 
                                  "totalAmount" ->  MongoDBObject("$sum" -> "$daysTaken"))))

  db.command(MongoDBObject("aggregate" -> "holidayRequests", "pipeline" -> pipeline))                            

}

should work.

Emiliano Martinez
  • 4,073
  • 2
  • 9
  • 19