[MongoDB Performance Optimization] How to Reduce Number of Queries When ]find

  mongodb, question

Hello, everyone. I’m a newcomer to mongodb. I now have a Post Collection with a created_at field and a creator field. I would like to make the following query now

db.posts.find({"creator" : ObjectId("xxxxx") }).sort("-created_at").limit(10)

As a result, I found that the database has traversed all my post. No matter what user goes through all post. I have already indexed created_at. I think I should only search some databases to return the results. May I know what is wrong with me?

Thank you!

Simply answer:

db.posts.createIndex({creator: 1, created_at: -1})

You need to know about indexes and how to use them.explainTo understand performance issues.