mongodb 索引类型有哪些?思维导图 代码示例(java 架构)-爱代码爱编程
MongoDB 提供了多种类型的索引来优化查询性能。不同的索引类型适用于不同的使用场景,选择合适的索引对于确保数据库的高效运行至关重要。下面我将提供一个关于 MongoDB 索引类型的思维导图概要,以及 Java 代码示例来展示如何创建和管理这些索引。
MongoDB 索引类型 - 思维导图概要
您可以创建一个以 “MongoDB 索引” 为核心节点的思维导图,并根据以下分类展开:
- 单字段索引
- 升序或降序索引
- 复合索引
- 多个字段组合排序
- 多键索引
- 支持数组字段中的每个元素
- 文本索引
- 用于全文搜索
- 地理空间索引
2d
和2dsphere
类型,用于地理位置数据
- 哈希索引
- 适用于散列查找
- TTL(Time-To-Live)索引
- 自动过期文档
- 部分索引
- 只对满足条件的文档进行索引
- 稀疏索引
- 忽略某些不包含索引字段的文档
- 唯一性约束
- 确保索引字段的值是唯一的
Java 代码示例
接下来是一些简单的 Java 代码示例,展示了如何在 MongoDB 中创建不同类型的索引。为了简化,假设我们已经有一个名为 sample
的集合,并且它包含了一些用户文档。
导入必要的包
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.conversions.Bson;
import static com.mongodb.client.model.Indexes.*;
import static com.mongodb.client.model.Indexes.ascending;
import static com.mongodb.client.model.Indexes.descending;
import static com.mongodb.client.model.IndexOptions.*;
连接到 MongoDB 并获取集合
public class MongoDBIndexExample {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> collection = database.getCollection("sample");
// ... 索引操作将在这里添加 ...
}
}
}
创建单字段索引
// 创建升序索引
Bson indexAscending = ascending("name");
collection.createIndex(indexAscending);
System.out.println("Created ascending index on 'name'.");
// 创建降序索引
Bson indexDescending = descending("age");
collection.createIndex(indexDescending);
System.out.println("Created descending index on 'age'.");
创建复合索引
// 创建复合索引,先按 age 降序再按 name 升序
Bson compositeIndex = indexes(descending("age"), ascending("name"));
collection.createIndex(compositeIndex);
System.out.println("Created composite index.");
创建多键索引
// 对数组字段 tags 创建多键索引
Bson multiKeyIndex = ascending("tags");
collection.createIndex(multiKeyIndex, new IndexOptions().name("tags_index"));
System.out.println("Created multi-key index on 'tags'.");
创建文本索引
// 创建文本索引用于全文搜索
Bson textIndex = text("description");
collection.createIndex(textIndex);
System.out.println("Created text index on 'description'.");
创建地理空间索引
// 创建 2dsphere 地理空间索引
Bson geoSpatialIndex = geo2DSphere("location");
collection.createIndex(geoSpatialIndex);
System.out.println("Created geospatial index on 'location'.");
创建哈希索引
// 创建哈希索引
Bson hashIndex = hashed("userId");
collection.createIndex(hashIndex);
System.out.println("Created hash index on 'userId'.");
创建 TTL 索引
// 创建 TTL 索引,设置过期时间为 3600 秒
Bson ttlIndex = ascending("createdAt");
collection.createIndex(ttlIndex, new IndexOptions().expireAfter(3600, TimeUnit.SECONDS));
System.out.println("Created TTL index on 'createdAt'.");
创建部分索引
// 创建只对 active 字段为 true 的文档有效的部分索引
Bson partialIndex = ascending("username");
Bson filter = eq("active", true);
collection.createIndex(partialIndex, new IndexOptions().partialFilterExpression(filter));
System.out.println("Created partial index on 'username'.");
创建稀疏索引
// 创建忽略缺失字段的稀疏索引
Bson sparseIndex = ascending("optionalField");
collection.createIndex(sparseIndex, new IndexOptions().sparse(true));
System.out.println("Created sparse index on 'optionalField'.");
添加唯一性约束
// 创建唯一性索引
Bson uniqueIndex = ascending("email");
collection.createIndex(uniqueIndex, new IndexOptions().unique(true));
System.out.println("Created unique index on 'email'.");
以上代码片段展示了如何使用 MongoDB Java 驱动程序创建各种类型的索引。请确保您已经在项目中正确配置了 MongoDB Java 驱动依赖,并且 MongoDB 服务正在运行并可访问。此外,请根据您的具体需求调整这些代码示例。索引是提高查询性能的关键工具,但过多的索引也可能导致写入性能下降,因此需要根据实际应用情况合理设计和管理索引。