MongoDB(index索引)

作者: 余生筑 | 来源:发表于2017-12-01 15:49 被阅读26次

时间度小则数据易读,空间度小则数据易写。索引的本质是用空间换取时间,所以创建过多的索引会导致数据易读而难写。

  • 创建按age升序排列的索引
db.users.createIndex({age:1})

返回结果

/* 1 */
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1.0
}
  • 查询索引
db.users.getIndexes()

返回结果

/* 1 */
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "what_i_love.users"
    },
    {
        "v" : 2,
        "unique" : true,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "what_i_love.users",
        "background" : true
    }
]

上图表示当前数据库一共提供两种索引方式,一种是按_id排序(默认索引),一种是按name排序

相关文章

  • MongoDB(index索引)

    时间度小则数据易读,空间度小则数据易写。索引的本质是用空间换取时间,所以创建过多的索引会导致数据易读而难写。 创建...

  • MongoDB中的定时索引

    MongoDB中存在一种索引,叫做TTL索引(time-to-live index,具有生命周期的索引),这种索引...

  • Mysql Innodb的索引结构:B+ Tree 聚簇

    索引 索引(Index)是帮助MySQL高效获取数据的数据结构。MongoDb索引使用BTree,而Mysql的M...

  • 24.Mongodb的索引操作

    Mongodb的索引操作 学习目标 掌握 mongodb索引的创建,删除操作 掌握 mongodb查看索引的方法 ...

  • MongoDB索引二(九)

    MongoDB索引二(九) 接上篇MongoDB索引一

  • MongoDB学习报告(二)

    概述 MongoDB索引管理MongoDB查询优化 MongoDB索引管理 单键索引中的每一项都应该对应被索引文档...

  • ElasticSearch初识(二)

    什么是正向索引、什么是倒排索引? 正向索引(forward index),反向索引(inverted index)...

  • 7. Interview-MySQL

    1 MySQL索引类型? 普通索引,index 主键索引,primary 唯一索引,unique index 全文...

  • MongoDB 索引 --- 2022-04-03

    本章介绍MongoDB索引,类似MYSQL,MongoDB也支持索引,区别是MongoDB支持对JSON结构的任意...

  • InnoDB,select会阻塞insert吗?

    InnoDB的索引有两类索引,聚集索引(Clustered Index)与普通索引(Secondary Index...

网友评论

    本文标题:MongoDB(index索引)

    本文链接:https://www.haomeiwen.com/subject/hhhbbxtx.html