首页 > [MongoDB性能优化]find的时候如何减少查询条数

[MongoDB性能优化]find的时候如何减少查询条数

大家好,我是mongodb新人。我现在有一个Post Collection, 里面有created_at字段和creator字段,我现在想如下查询

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

结果我发现数据库遍历了我所有的post。 无论什么用户都是遍历所有post。我已经给created_at索引了。 我自己觉得应该只要搜索部分数据库就能返回结果了。 请问是我哪里出了问题?

谢谢!


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

简单地回答:

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

你需要了解索引相关的知识和如何使用explain来了解性能问题。

【热门文章】
【热门文章】