目录
一、MongoDB官网地址
- 一、MongoDB官网地址
- 二、Hash索引(Hashed Indexes)的概述
- 三、创建Hash索引(Hashed Indexes)的语法
- 四、创建Hash索引(Hashed Indexes)的示例
- 4.1、数据准备
- 4.2、创建Hash索引(Hashed Indexes)
- MongoDB官网地址:https://www.mongodb.com/docs/manual/core/index-single/
- 不同于传统的B-Tree索引,哈希索引使用hash函数来创建索引。
- 在索引字段上进行精确匹配,但不支持范围查询,不支持多键hash。
- Hash索引上的入口是均匀分布的,在分片集合中非常有用。
-
语法
db.collection.createIndex({fieldName: 'hashed'})
-
准备数据集,执行脚本
db.stores.insert( [ { _id: 1, name: "Java Hut", description: "Coffee and cakes" }, { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" }, { _id: 3, name: "Coffee Shop", description: "Just coffee" }, { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing"}, { _id: 5, name: "Java Shopping", description: "Indonesian goods" } ] )
-
查看初始化的数据
> db.stores.find()
-
创建name的Hash索引
db.stores. createIndex({name : 'hashed'})
-
查看创建的全文索引
> db.stores.getIndexes()