node.js - MongoDB Speed up grouping -


i have basic collection fruits follows schema this:

{_id:1234 type:'apple'  store:'abc'} {_id:1235 type:'apple'  store:'xyz'} {_id:1236 type:'banana' store:'xyz'} {_id:1237 type:'pear'   store:'abc'} {_id:1238 type:'pear'   store:'xyz'} {_id:1239 type:'apple'  store:'abc'} {_id:1240 type:'banana' store:'abc'} {_id:1241 type:'apple'  store:'xyz'} 

i wish group , sum fruit have aggregate so:

app.get('/getfruittotals', function(req, res){     db.fruit.aggregate([{$group:{_id:$type, total:{$sum:1}}}], function(err, results){         res.send(results);     });  }) 

this works fine except collection couple million records simple grouping takes 4000 milliseconds.

if run instead:

 db.fruit.find({type:'apple'}).count() 

i can cut query time down 4000 milliseconds 110 milliseconds. since have 3 possible types of fruit (or should say, possible types of fruit known me) can run query 3 times in 1/10th of the time takes aggregate. there way speed $group? far know indexing doesn't $group aggregate.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -