Dove
Dove

Reputation: 125

Mongodb toArray() performance

I have a collection 'matches' with 727000 documents inside. It has 6 fields inside, no arrays just simple integers and object Ids. I am doing query to collection as follows:

matches.find({
  $or: [{
      homeTeamId: getObjectId(teamId)
    }, {
      awayTeamId: getObjectId(teamId)
    }
  ],
  season: season,
  seasonDate: {
    '$gt': dayMin,
    '$lt': dayMax
  }
}).sort({
  seasonDate: 1
}).toArray(function (e, res) {
  callback(res);
});

Results returning only around 7-8 documents. The query takes about ~100ms, which i think is quite reasonable, but the main problem is, when i call method toArray(), it adds about ~600ms!! I am running server on my laptop, Intel Core I5, 6GB RAM but i can't believe it adds 600ms for 7-8 documents. Tried using mongodb-native driver, now switched to mongoskin, and stil getting the same slow results. Any suggestions ?

Upvotes: 3

Views: 3816

Answers (1)

Moacy Barros
Moacy Barros

Reputation: 1947

toArray() method iterate throw all cursor element and load them on memory, it is a highly cost operation. Maybe you can add index to improve your query performance, and/or avoid toArray iterating yourself throw the Cursor.

Regards, Moacy

Upvotes: 3

Related Questions