mongodb学习(2)索引有关

  1. _id索引
    _id索引是绝大多数集合默认建立的索引,对于每个插入的数据,MongDB都会自动生成一条唯一的_id字段,db.posts.getIndexes()可以获取到该集合的索引,创建一个集合之后,系统会默认给这个集合创建一个_id索引
db.posts.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "posts.posts"
    }
]
  1. 普通索引
    创建普通索引:

db.collection.createIndex(keys, options)

语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。创建索引后可以加快查询速度。

可以对文档中的多个键单独创建索引或者创建联合索引。

  1. MongoDB 高级索引
    1)索引子文档字段:可以对文档的子文档创建索引
> db.col.insert({
...    "address": {
...       "city": "Los Angeles",
...       "state": "California",
...       "pincode": "123"
...    },
...    "tags": [
...       "music",
...       "cricket",
...       "blogs"
...    ],
...    "name": "Tom Benzamin"
... })
WriteResult({ "nInserted" : 1 })

> db.col.ensureIndex({"address.city":1,"address.state":1,"address.pincode":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 5,
    "numIndexesAfter" : 6,
    "ok" : 1
}

> db.col.find({"address.city":"Los Angeles"})
{ "_id" : ObjectId("5c9c581279239019d92fa21a"), "address" : { "city" : "Los Angeles", "state" : "California", "pincode" : "123" }, "tags" : [ "music", "cricket", "blogs" ], "name" : "Tom Benzamin" }

2)数组索引
可以对文档中的数据建立索引:

> db.posts.find()
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
{ "_id" : ObjectId("5c9c5c2079239019d92fa21c"), "post_text" : "enjoy the mongodb articles on monday" }
{ "_id" : ObjectId("5c9c6f1d79239019d92fa21e"), "name" : "hi,today is sunny day,today is Monday" }
{ "_id" : ObjectId("5c9c729879239019d92fa21f"), "name" : "lihong" }
{ "_id" : ObjectId("5c9c780a79239019d92fa220"), "name" : "liming" }
> db.posts.ensureIndex({"tags":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 2,
    "note" : "all indexes already exist",
    "ok" : 1
}
> db.posts.find({tags:"mongodb"})
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
  1. 全文检索
    全文检索对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。
    开启全文检索
> db.adminCommand({setParameter:true,textSearchEnabled:true})
{
    "ok" : 0,
    "errmsg" : "attempted to set unrecognized parameter [textSearchEnabled], use help:true to see options "
}

创建全文索引:先在集合posts中插入两个文档,再对文档中的post_text字段建立索引,然后执行搜索

> db.posts.insert({
...    "post_text": "enjoy the mongodb articles on Runoob",
...    "tags": [
...       "mongodb",
...       "runoob"
...    ]
... })
WriteResult({ "nInserted" : 1 })
> db.posts.insert({"post_text": "enjoy the mongodb articles on monday"})
WriteResult({ "nInserted" : 1 })
> db.posts.ensureIndex({post_text:"text"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.posts.find({$text:{$search:"enjoy"}})
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
{ "_id" : ObjectId("5c9c5c2079239019d92fa21c"), "post_text" : "enjoy the mongodb articles on monday" }

查找索引名:

> db.posts.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "posts.posts"
    },
    {
        "v" : 2,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "post_text_text",
        "ns" : "posts.posts",
        "weights" : {
            "post_text" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 3
    }
]

删除全文检索:

db.posts.dropIndex("post_text_text")

索引是特殊的数据结构,以易于遍历的形式存储数据集的一小部分。 索引存储特定字段或一组字段的值,按照索引中指定的字段值排序。
使用索引,可以加快索引相关的查询,也相应地带来一些坏处:
每个索引占据一定的存储空间,在进行插入,更新和删除操作时也需要对索引进行操作。所以,如果你很少对集合进行读取操作,建议不使用索引。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、MongoDB简介 1.概述 ​ MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WE...
    郑元吉阅读 1,021评论 0 2
  • 1. 介绍、安装、使用(简单写写,不做详细介绍) 1.1 介绍 Mongodb是属于NoSql的一种数据类型; M...
    Grace_ji阅读 1,591评论 0 0
  • 转 # https://www.cnblogs.com/easypass/archive/2010/12/ 08/...
    吕品㗊阅读 9,884评论 0 44
  • 目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" 、"$gte"...
    彩虹之梦阅读 1,070评论 0 1
  • 索引是应用程序设计和开发的一个重要方面。 若索引太多, 应用程序的性能可能会受到影响。 而索引太少, 对查询性能又...
    好好学习Sun阅读 1,068评论 0 4