This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

Optimizing mongodb queries for the first time.

Words
84
Reading
1 min
Listen
Play
9y

https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/ is the first place, where I knew, I can do something. The below snippet allowed me to test & verify the efforts quickly.

db.setProfilingLevel(0)
db.system.profile.drop()
db.setProfilingLevel(1, 100); //100, 500, 1000, 2000
db.system.profile.find({}, {query: 1, ns: 1, millis: 1}).limit(10).sort( { millis : -1 } ).pretty()

Query explainer: db.collection.find(query).explain('executionStats');

Learnt complex/compound queries can make use of individual indexes.

Available indexes & their sizes can be seen using db.collection.stats()

mongotop a neat tool.

Hopefully, this would be a real optimization.

Note to self:

  1. Keep an eye on index size
  2. Try to rewrite logic & incorporate optmization at design level. So, the queries will be faster with only default and simple indexes.
Optimizing mongodb queries for the first time. | Ecency