hiepnv
hiepnv

Reputation: 1

MongoDB takes long time to query

I have a collection which over 10000 documents. Each document is about 10KB. When I run this query:

News
.find({topics: { '$elemMatch': { '$in': ['5606059d924327636fe3e150'] } }, state: 'APPROVED', is_removed: false})

it takes me around 8 seconds to finish the query.

I have used the indexes for the fields: topics, state and is_removed. The explain() query returned: "millis" : 45.

I think the result of query is quite large (10000*10KB) so it will take time to pull the data right?

Please help me to explain this and show me how to reduce the query time.

Thank you!

Upvotes: 0

Views: 980

Answers (1)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

For starters: Your query is unnecessarily complicated.

db.collection.find({
    topics:{ "$in":["5606059d924327636fe3e150","5606059d924327636fe3e151"]},
    state:"APPROVED",
    is_removed: false
})

Furthermore, you might want to have an index:

db.collection.createIndex({ topics:1, state:1, is_removed:1 })

Upvotes: 1

Related Questions