3

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 ?

3
  • What does callback() do? Commented Apr 8, 2013 at 21:28
  • It returns data from function. I have wrapped this query inside function exports.getPrevNextMatches = function(teamId, callback){} . But when i do the same without toArray() method, to return a cursor of results, it takes as mentioned ~100 ms. toArray() method adds ~600ms Commented Apr 8, 2013 at 21:31
  • 1
    did you try forEach on the find result instead of toArray? I doubt it will help, but it's an easy thing to try. Also, what's explain give you for that query? Commented Apr 9, 2013 at 0:32

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.