目录

一、MongoDB官网地址二、Hash索引(Hashed Indexes)的概述三、创建Hash索引(Hashed Indexes)的语法四、创建Hash索引(Hashed Indexes)的示例4.1、数据准备4.2、创建Hash索引(Hashed Indexes)

一、MongoDB官网地址

MongoDB官网地址:https://www.mongodb.com/docs/manual/core/index-single/

二、Hash索引(Hashed Indexes)的概述

不同于传统的B-Tree索引,哈希索引使用hash函数来创建索引。在索引字段上进行精确匹配,但不支持范围查询,不支持多键hash。Hash索引上的入口是均匀分布的,在分片集合中非常有用。

三、创建Hash索引(Hashed Indexes)的语法

语法 db.collection.createIndex({fieldName: 'hashed'})

四、创建Hash索引(Hashed Indexes)的示例

4.1、数据准备

准备数据集,执行脚本 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()

4.2、创建Hash索引(Hashed Indexes)

创建name的Hash索引 db.stores. createIndex({name : 'hashed'})

查看创建的全文索引 > db.stores.getIndexes()

查看原文