katch
katch

Reputation: 930

Using Javascript Mongodb driver for Aggregation

I am trying to extract aggregate data from Mongodb for each filename each version where platform is win

My data has following format

[
{ "_id" : { "platform" : "mac", "filename" : "f1.json", "version" : "1.4.14" }, "avgSecs" : 15 },
{ "_id" : { "platform" : "win", "filename" : "f2.json", "version" : "3000.0.0_developer" }, "avgSecs" : 1217 },
{ "_id" : { "platform" : "win", "filename" : "f3.json", "version" : "1.4.14" }, "avgSecs" : 1711 },
{ "_id" : { "platform" : "win", "filename" : "f3.json", "version" : "3000.0.0_developer" }, "avgSecs" : 1191 }
]

My code is as follows

  [ {$sort:{filename:1}},{$match:{platform:"win"}},{ $group:{ _id: {filename:"$filename", version:"$version"}, avgSecs: {$avg:"$int_secs"} } }]

which is send to mongodb using JS apis as follows

        collection.aggregate(aggQuery).toArray(function(err,docs){
            assert.equal(null,err)
            resolve(docs);
        })

but I dont get parsed and aggregate data but complete data as is from the db.

I have checked the same aggregate query works fine with mongodb shell. I am not sure what is the problem with JS api. Am I using it the right way

Upvotes: 0

Views: 43

Answers (1)

katch
katch

Reputation: 930

Ok found it.

AggQuery was sent in as string while it was expecting an Array Object. Made the object Array and now results are as expected.

Upvotes: 1

Related Questions