technosis
technosis

Reputation: 87

MongoDB query fast in mongo shell but slow/times out in nodejs

The following query takes approximately 54ms in the mongo shell, but times out after a minute in the nodejs driver:

db.posts.find(
  {
    parentId: 10,
    modifiedGmt: {
      "$gte": new Date("2017-01-01T00:00:00.000z")
    },
    postType: {
      "$in": ["type1", "type2"]
    }
  },
  {
    _id: 1,
    parentId: 1,
    postId: 1,
    url: 1,
    modifiedGmt: 1,
    authors: 1,
    taxonomy: 1,
    postType: 1
  }
).sort({modifiedGmt: 1}).limit(2400)

Explain shows that the query is using existing indexes. If I drop the limit to something very low like 10, it won't time out but it will take far, far too long. I'm not really sure where to go with this. It's a large collection but with the indexes in place and the limit sub-10000 I don't see why it would be so slow.

Any ideas?

Upvotes: 0

Views: 572

Answers (1)

Ruud van den Boomen
Ruud van den Boomen

Reputation: 607

I'm suspecting that the .sort is not able to use your index.

I would recommend to have a read on this page sort-operator-and-performance.

Upvotes: 1

Related Questions