MongoDB索引简介与使用

1、B树索引(B-tree Index)

优点:支持高效的范围查询和排序。
缺点:索引的大小较大,对于频繁更新的集合会有一定的维护开销。
使用场景:适用于大部分查询场景。

db.collection.createIndex({ field: 1 })
MongoTemplate mongoTemplate = new MongoTemplate();
Index index = new Index().on("field", Sort.Direction.ASC);
mongoTemplate.indexOps("collection").ensureIndex(index);

2、散列索引(Hashed Index)

优点:可以快速查找索引键值。
缺点:无法按照索引键的顺序进行排序和范围查询。
使用场景:适用于均匀分布的键值和快速查找索引键值的场景。

db.collection.createIndex({ field: "hashed" })
MongoTemplate mongoTemplate = new MongoTemplate();
HashIndexed index = new HashIndexed("field");
mongoTemplate.indexOps("collection").ensureIndex(index);

3、文本索引(Text Index)

优点:可以快速进行文本搜索和匹配。
缺点:索引大小较大,对索引的维护和更新操作会有一定的性能开销。
使用场景:适用于进行文本搜索和匹配的场景,如博客、新闻和论坛等。

db.collection.createIndex({ field: "text" })
MongoTemplate mongoTemplate = new MongoTemplate();
TextIndexed index = new TextIndexed("field");
mongoTemplate.indexOps("collection").ensureIndex(index);

4、全文复合索引(Compound Text Index)

优点:可以同时对多个文本字段进行搜索和排序。
缺点:索引大小较大,对索引的维护和更新操作会有一定的性能开销。
使用场景:适用于需要对多个文本字段进行搜索和排序的场景。

db.collection.createIndex({ field1: "text", field2: "text" })
MongoTemplate mongoTemplate = new MongoTemplate();
TextIndexDefinition textIndex = new TextIndexDefinition.TextIndexDefinitionBuilder()
  .onField("field1")
  .onField("field2")
  .build();
mongoTemplate.indexOps("collection").ensureIndex(textIndex);

5、地理空间索引(Geospatial Index)

优点:可以快速进行地理空间查询、范围查询和邻近查询。
缺点:无法支持复杂的几何计算。
使用场景:适用于处理地理位置数据的场景,如地图应用、位置服务等。

db.collection.createIndex({ locationField: "2dsphere" })
//MongoDB支持多种地理空间索引类型,如2dsphere、2d、geoHaystack等。可以根据实际需求选择合适的索引类型。
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(Store.class);
Index index = new GeospatialIndex("location");
indexOps.ensureIndex(index);

6、二维球体索引(2dsphere Index)

优点:可以快速进行二维球体上的地理位置查询和距离计算。
缺点:无法支持复杂的几何计算。
使用场景:适用于处理地球表面上的地理位置数据的场景。

db.collection.createIndex({ locationField: "2dsphere" })
MongoTemplate mongoTemplate = new MongoTemplate();
GeospatialIndex index = new GeospatialIndex("location");
index.typed(GeoSpatialIndexType.GEO_2DSPHERE);
mongoTemplate.indexOps("collection").ensureIndex(index);

7、TTL索引(Time To Live Index)

优点:方便进行数据的自动清理和过期管理。
缺点:会增加对索引的维护开销。
使用场景:适用于需要自动清理过期数据的场景,如日志记录。

db.collection.createIndex({ expireField: 1 }, { expireAfterSeconds: 3600 })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(Log.class);
indexOps.ensureIndex(new Index("createdAt", Direction.DESC).expire(3600));

8、二进制数据索引(Binary Data Index)

优点:方便进行二进制数据的查找和查询。
缺点:索引的大小较大,对于频繁更新的二进制数据会有一定的维护开销。
使用场景:适用于需要对二进制数据进行查找和管理的场景,如图片库和多媒体数据库。

db.collection.createIndex({ binaryField: "hashed" })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(File.class);
indexOps.ensureIndex(new Index().on("data", Direction.ASC).binary());

9、散列分片索引(Hashed Sharding Index)

优点:可以将数据均匀分布在不同的分片中,支持大规模数据存储和查询。
缺点:无法支持范围查询和排序。
使用场景:适用于需要水平扩展和高吞吐量的场景,如大型分布式系统中的数据存储和查询。

db.collection.createIndex({ shardKeyField: "hashed" })
MongoTemplate mongoTemplate = new MongoTemplate();
HashedShardingIndex index = new HashedShardingIndex("shardKeyField");
mongoTemplate.indexOps("collection").ensureIndex(index);

10、复合散列索引(Compound Hashed Index)

优点:可以同时对多个字段进行散列索引,并进行高效的查找操作。
缺点:无法支持范围查询和排序。
使用场景:适用于需要对多个字段进行组合索引并进行散列的场景。

db.collection.createIndex({ field1: 1, field2: 1, ... }, { shardKey: "hashed" })
MongoTemplate mongoTemplate = new MongoTemplate();
CompoundHashIndexed index = new CompoundHashIndexed(Arrays.asList("field1", "field2", ...));
mongoTemplate.indexOps("collection").ensureIndex(index);

11、单字段索引(Single Field Index)

优点:适用于在单个字段上快速查询和排序数据。可以提高查询性能。
缺点:不能处理多个字段之间的查询关系。
使用场景:适用于对单个字段进行频繁查询和排序的场景,例如根据用户名查询用户信息或按创建时间排序文档。

db.collection.createIndex({ field: 1 })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(User.class);
indexOps.ensureIndex(new Index("username", Direction.ASC).unique());

12、复合索引(Compound Index)

优点:可以处理多个字段之间的查询关系,提高查询性能。
缺点:索引会占用更多的磁盘空间。
使用场景:适用于需要在多个字段上进行复杂查询和排序的场景,例如根据用户年龄和名称查询用户信息。

db.collection.createIndex({ field1: 1, field2: -1 })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(User.class);
indexOps.ensureIndex(new Index().on("field1", Direction.ASC).on("field2", Direction.DESC));

13、唯一索引(Unique Index)

优点:保证字段的唯一性,可以有效地防止重复数据插入。
缺点:索引会增加写入数据的开销。
使用场景:适用于需要保证字段唯一性的场景,例如用户的邮箱或手机号码必须唯一。

db.collection.createIndex({ field: 1 }, { unique: true })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(User.class);
indexOps.ensureIndex(new Index("email").unique());

14、稀疏索引(Sparse Index)

优点:只为存在索引字段的文档创建索引,减少了索引的大小。
缺点:查询性能可能会受到影响。
使用场景:适用于索引字段不是所有文档都存在的场景,例如文档的某个可选字段。

db.collection.createIndex({ field: 1 }, { sparse: true })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(User.class);
indexOps.ensureIndex(new Index().on("category", Direction.ASC).sparse());

15、 排序索引(Sort Index)

优点:可以快速地按指定字段进行排序。
缺点:索引会占用更多的磁盘空间。
使用场景:适用于需要频繁按某个字段进行排序的场景,例如按照分数对学生进行排名。

db.collection.createIndex({ field: 1 })
MongoTemplate mongoTemplate = new MongoTemplate();
IndexOperations indexOps = mongoTemplate.indexOps(User.class);
indexOps.ensureIndex(new Index().on("score", Direction.ASC).partial("score > 0"));

16、数组索引(Array Index)

优点:

  • 快速查询:在数组字段上创建索引可以提高查询数组元素的速度,特别是在数组中包含大量元素的情况下。
  • 支持数组操作:创建索引后,可以使用数组操作符(如 e l e m M a t c h 、 elemMatch、 elemMatchall等)来优化查询。

缺点:

  • 索引大小增加:对数组字段创建索引会增加索引的大小,占用更多的存储空间。
  • 写入性能下降:更新数组字段时,需要更新索引,可能会导致写入操作的性能下降。
  • 查询限制:对于需要精确匹配数组中的多个元素的查询,可能需要使用复合索引或其他查询方式。

使用场景:

  • 对数组字段进行频繁的查询和排序操作。
  • 需要使用数组操作符(如$ elemMatch、$all等)进行数组元素的匹配和查询。
  • 数组字段中的元素个数较大,需要提高查询性能。
db.collection.createIndex({ "field.$": 1 })
MongoTemplate mongoTemplate = new MongoTemplate();
Index index = new Index().on("field.$", Sort.Direction.ASC);
mongoTemplate.indexOps("collection").ensureIndex(index);
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值