MongoDB在创建集合的时候就在_id字段创建了一个唯一性索引. 这个索引防止客户端插入两个拥有相同_id的文档,并且这个索引不可被删除。
MongoDB索引使用的是B-tree数据结构。
创建索引
db.collection.createIndex()
单个字段创建索引
db.books.createIndex({name:1},{ unique: true })
#在name上创建唯一性索引
复合索引
MongoDB规定限制复合的字段最多只能是31个。
db.userlist.createIndex({name:1,score:1})
多键索引
1.数组元素
db.userlist.createIndex({"habbit":1});
2.含有文档的数组元素
db.userlist.createIndex({"books.name":1,"books.pages":1})
文本索引
1.创建文本索引
db.news.createIndex({title:'text',content:'text'})
db.collection.createIndex( { "$**": "text" } )
#创建多个
指定索引名称:
db.news.createIndex({title:'text',content:'text'},{name:'tilte_content_index'})
根据索引名称删除索引:
db.collection.dropIndex("tilte_content_index")
3.数据准备
db.news.insertMany([
{title:'aa bb cc ',content:'cc rice'},
{title:'bb cc ',content:'dd ee'},
{title:'ee bb aa ',content:'cc'},
{title:'aa dd',content:'ee'},
{title:'aa bb',content:'ae'},
{title:'rice cc dd ',content:'aa'}
])
4.检索:
- 单个关键字
db.news.find({$text:{$search:"aa"}})
查找结果:
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa1"), "title" : "aa bb cc ", "content" : "cc rice" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa6"), "title" : "rice cc dd ", "content" : "aa" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa5"), "title" : "aa bb", "content" : "ae" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa4"), "title" : "aa dd", "content" : "ee" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa3"), "title" : "ee bb aa ", "content" : "cc" }
- 多个关键字(或关系):空格隔开
db.news.find({$text:{$search:"aa cc"}})
显示结果:
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa2"), "title" : "bb cc ", "content" : "dd ee" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa1"), "title" : "aa bb cc ", "content" : "cc rice" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa6"), "title" : "rice cc dd ", "content" : "aa" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa5"), "title" : "aa bb", "content" : "ae" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa4"), "title" : "aa dd", "content" : "ee" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa3"), "title" : "ee bb aa ", "content" : "cc" }
- 多个关键字(与关系): \"包住关键字
db.news.find({$text:{$search:"\"aa\" \"cc\""}})
查询结果:
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa1"), "title" : "aa bb cc ", "content" : "cc rice" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa6"), "title" : "rice cc dd ", "content" : "aa" }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa3"), "title" : "ee bb aa ", "content" : "cc" }
- 查询多个关键,排除某个关键字
db.news.find({$text:{$search:"\"aa\" \"cc\" -rice"}})
显示结果:
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa3"), "title" : "ee bb aa ", "content" : "cc" }
相似度查询:
db.news.find(
{"$text":{"$search":"aa cc"}},
{"score":{"$meta":"textScore"}}
).sort({"score":
{"$meta":"textScore"}
});
查询结果:
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa1"), "title" : "aa bb cc ", "content" : "cc rice", "score" : 2.083333333333333 }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa3"), "title" : "ee bb aa ", "content" : "cc", "score" : 1.7666666666666666 }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa6"), "title" : "rice cc dd ", "content" : "aa", "score" : 1.7666666666666666 }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa2"), "title" : "bb cc ", "content" : "dd ee", "score" : 0.75 }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa4"), "title" : "aa dd", "content" : "ee", "score" : 0.75 }
{ "_id" : ObjectId("599f03272aa12cb8dc4dcaa5"), "title" : "aa bb", "content" : "ae", "score" : 0.75 }
2d球面索引
2d索引
1.创建索引
db.shop.createIndex({"loc":"2d"})
2.插入数据
db.shop.insertMany([{name:'兰州拉面',loc:[11,12]},
{name:'番茄妹米线',loc:[11,11]},
{name:'香格里拉',loc:[10,12]},
{name:'座上客',loc:[11,52]},
{name:'苏果超市',loc:[41,42]},
{name:'一鸣奶品',loc:[31,32]},
{name:'和善园',loc:[21,22]}]);
3.检索:
db.shop.find({loc:{$near:[11,11],$maxDistance:5}});
4.检索结果:
{ "_id" : ObjectId("599f192d2aa12cb8dc4dcaaf"), "name" : "番茄妹米线", "loc" : [ 11, 11 ] }
{ "_id" : ObjectId("599f192d2aa12cb8dc4dcaae"), "name" : "兰州拉面", "loc" : [ 11, 12 ] }
{ "_id" : ObjectId("599f192d2aa12cb8dc4dcab0"), "name" : "香格里拉", "loc" : [ 10, 12 ] }