简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM