简体   繁体   中英

Query vs filter in mongodb and node.js peformance

I am using following two ways to get count of some documents by status. Here is my two ways:-

collection.find({ Status: 'pending' }).count(function (e, pending) {
collection.find({ Status: 'open' }).count(function (e, open) {
collection.find({ Status: 'solved' }).count(function (e, solved) {
     var obj={
         solved:solved,
         open:open,
         pending:pending
     }
     res.send(obj);
   });
  });
});

and Second is:-

collection.find().toArray(function(e,data){
var open=data.filter(function(f){f.Status="open"}).length;
var solved=data.filter(function(f){f.Status="solved"}).length;
var pending=data.filter(function(f){f.Status="pending"}).length;
 var obj={
        solved:solved,
    open:open,
    pending:pending
      }
 res.send(obj);
});

I have loop that will run this code 5 times(this is just a sample code in real I have some other conditions based on loop) and return me result . But I don't know which is the better way to do this. Please help me to chose the best way which is better for performance. Thanks

What you have here is a tradeoff between bandwidth and latency.

The first creates 3 queries to the database which means 3 network roundtrips from application to database and back. However, each roundtrip only returns a single number.

The second has just one round-trip, but it downloads the whole content of the collection.

Which is the better solution depends on your network infrastructure and what latency and bandwidth it provides.

However, there is also another method which gives you the best of both worlds, and this is to use an aggregation pipeline to calculate the count of all fields on the database. Here an untested example:

db.collection.aggregate([
     // first project each document into a new one with separate 
     // fields for "open", "pending" and "solved" where the current
     // status gets the value "1" and all others the value "0"
     {$project: {
        open: {
           $cond: [ { $eq: [ "$Status", "open" ] }, 1, 0 ]
        },
        pending: {
           $cond: [ { $eq: [ "$Status", "pending" ] }, 1, 0 ]
        },
        solved: {
           $cond: [ { $eq: [ "$Status", "solved" ] }, 1, 0 ]
        }
     }},
     // then create a new document by creating the sum of the created document
     {$group: {
        _id:1,  // group all documents into one with the id "1"
        open: { $sum: "$open" },
        solved: { $sum: "$solved" },
        pending: { $sum: "$pending" }
     }},

]);

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