【发布时间】:2013-12-18 17:57:27
【问题描述】:
我正在尝试了解如何最好地使用 MongoDB 中的索引。假设我有一组像这样的文档:
{
_id: 1,
keywords: ["gap", "casual", "shorts", "oatmeal"],
age: 21,
brand: "Gap",
color: "Black",
gender: "female",
retailer: "Gap",
style: "Casual Shorts",
student: false,
location: "US",
}
我会定期运行查询以查找与每个字段的一组条件匹配的所有文档,例如:
db.items.find({ age: { $gt: 13, $lt: 40 },
brand: { $in: ['Gap', 'Target'] },
retailer: { $in: ['Gap', 'Target'] },
gender: { $in: ['male', 'female'] },
style: { $in: ['Casual Shorts', 'Jeans']},
location: { $in: ['US', 'International'] },
color: { $in: ['Black', 'Green'] },
keywords: { $all: ['gap', 'casual'] }
})
我试图弄清楚我可以创建什么样的索引来提高这样的查询速度。我应该像这样创建一个复合索引:
db.items.ensureIndex({ age: 1, brand: 1, retailer: 1, gender: 1, style: 1, location: 1, color: 1, keywords: 1})
或者我可以创建一组更好的索引来优化这个查询?
【问题讨论】:
标签: mongodb database-performance query-performance indices