记得刚学 MongoDB 的时候,被它的 no schema 设计所震撼,能以 JSON 的形式存储文档,同时还能检索、排序、聚合,支持各种操作,同时发现性能好像也不错,感觉发现了新大陆。
不过随着对 MongoDB 越来越了解,它也有索引机制,且原理和其他数据库索引也差不多。
那什么是索引呢?本质上是一种提高查询效率的机制,但其实也是一种数据结构,包含搜索键和数据指针,前者存储要搜索的值,后者指针指向数据所在的块。
下面这个例子中,索引通过检索最终扫描特定数据块而不是整个文档,以空间换效率。

那索引在 MongoDB 中具体有什么好处呢?
1:肯定能提升性能,通过索引能够快速定位数据,而不是扫描整数据集。
2:提高读取效率,比如检索、排序、聚合都因为有了索引而提升了查询效率。
3:支持排序,不管是什么数据库,排序需求非常多,而索引能够进行高效的排序。
4:支持唯一性,确保字段有不同的值,满足了应用需要。
5:优化范围查询,范围筛选可以说是索引机制最大的受益者。
6:索引能够让文本高效搜索,看作一个搜索引擎。
7:索引能够高效的路由,从而提高分片集群的性能。
总之,如果没有索引,MongoDB 会对整个集合扫描,检查集合中的每个文档以查找匹配结果,如果数据量很大,那么耗时将非常大!
MongoDB 中可以不了解索引是内部如何工作的,但要学会如何使用索引,从而提升应用程序性能。
MongoDB 大概有几种索引!
1:创建单个索引
db.users.createIndex({ age: 1 });
db.users.find({ age: { $gt: 25 } }).explain("executionStats");
2:创建复合索引
db.orders.createIndex({ customerId: 1, orderDate: -1 });
db.orders.find({
customerId: "user123",
orderDate: { $gte: ISODate("2023-01-01") }
})
3:多键索引(Multikey Index)
MongoDB 中如果某个字段存储数组,可以对数组中的每个元素分别索引。
db.products.createIndex({ tags: 1 });
db.products.insertMany([
{
name: "足球",
tags: ["球类", "团体",
},
{
name: "网球",
tags: ["球类", "单人"],
},
{
name: "短跑100米",
tags: ["单人", "田径"],
}
]);
# 必须同时包含
db.products.find({ tags: { $all: ["团体", "球类"] } });
4:嵌套索引
MongoDB 中字段能存储 JSON 对象,可以给特定key或嵌套建立索引。
db.users.createIndex({ address.city: 1 });
db.users.insertMany([{ name: "张三", address: { city: "北京", zip: "10001" } }])
db.users.find({ "address.city": "北京" });
如果嵌套字段是一个数组,且数组中的元素是 JSON 对象,你可以为数组元素中的某个键创建多键索引:
db.users.insertMany([
{ name: "张三", address: [{ city: "北京", zip: "10001" }, { city: "上海", zip: "20001" }]}
]);
db.users.createIndex({ "address.city": 1 });
db.users.find({ "address.city": "北京" });
5:文本索引
可以简单实现一个搜索引擎:
db.articles.createIndex({ content: "text" });
db.articles.find({ $text: { $search: "indexing" } });
最后体验一个 mongoDB 借助索引实现高效的聚合、排序、汇总。
db.articles.insertMany([
{ title: "MongoDB Basics", tags: ["database", "nosql"] },
{ title: "Advanced MongoDB", tags: ["database", "performance"] },
{ title: "MongoDB Indexing", tags: ["database", "indexing"] },
{ title: "NoSQL Overview", tags: ["nosql", "trends"] }
]);
db.articles.createIndex({ tags: 1 });
db.articles.aggregate([
{ $unwind: "$tags" },
// 按 tags 分组,并统计数量
{ $group: { _id: "$tags", count: { $sum: 1 } } },
// 按数量降序排序
{ $sort: { count: -1 } }
]);
最后的结果:
[
{"_id":"database","count":3},
{"_id":"nosql","count":2},
{"_id":"performance","count":1},
{"_id":"indexing","count":1},
{"_id":"trends","count":1}
]
对于技术,了解干什么的,知道什么是重要的,根据原则有效去使用,这样就达到了基本目的!




