Official MongoDB Scala Driver, aggregate count sum 1

1.1k Views Asked by At

I am using official mongo scala driver: http://mongodb.github.io/mongo-scala-driver/.

I want to do query like:

db.test.aggregate([{"$group" : {_id:{name:"$name",details:"$details.id"}, count:{$sum:1}}}, {$sort:{"count":-1}} ])

so in scala code I am doing:

collectionDoc.aggregate(List(
      group(Document("name" -> "$name", "details" -> "$details.id"), Accumulators.sum("count", "1")),
      )).toFuture()

but in all results I see:

(count,BsonInt32{value=0}))

From mongo driver logs I see that its send:

{
  "aggregate": "test",
  "pipeline": [
    {
      "$group": {
        "_id": {
          "name": "$name",
          "details": "$details.id"
        },
        "count": {
          "$sum": "1"
        }
      }
    }
  ],
  "cursor": {
    "batchSize": 2147483647
  },
  "$db": "my-db",
  "$readPreference": {
    "mode": "primaryPreferred"
  }
}

If I do this query in mongo it counts those records fine.. Any idea how to solve this ?

Thanks!

1

There are 1 best solutions below

0
On

Ah silly mistake. Ive put in scala code "1" instead of 1. I found the issue after setting profile level to 2, and compared both queries.