美文网首页
mongodb索引操作

mongodb索引操作

作者: 威研威语 | 来源:发表于2017-01-05 23:07 被阅读0次

查看索引情况

db.imooc_collection.getIndexes()

> show tables

> db.imooc_collection.getIndexes()[ ]

> db.imooc_collection.insert({x:1})

WriteResult({"nInserted":1})

> db.imooc_collection.find()

{"_id": ObjectId("586df1ba46208ecdd6d08b36"),"x":1}

> db.imooc_collection.getIndexes()

[{"v":2,"key": {"_id":1},"name":"_id_","ns":"imooc.imooc_collection"}]

创建索引

db.imooc_collection.ensureIndex({x:1})

> db.imooc_collection.ensureIndex({x:1}){"createdCollectionAutomatically":false,"numIndexesBefore":1,"numIndexesAfter":2,"ok":1}>

索引可以重复创建,对于已经创建的索引会直接返回成功

> db.imooc_2.getIndexes()

[{"v":2,"key": {"_id":1},"name":"_id_","ns":"imooc.imooc_2"},{"v":2,"key": {"x":1},"name":"x_1","ns":"imooc.imooc_2"}]

> db.imooc_2.ensureIndex({x:1}){"createdCollectionAutomatically":false,"numIndexesBefore":2,"numIndexesAfter":2,"note":"all indexes already exist","ok":1}

> db.imooc_2.find()

{"_id": ObjectId("586df44f46208ecdd6d08b37"),"x":1}

创建复合索引

> db.imooc_2.ensureIndex({x:1,y:1}){"createdCollectionAutomatically":false,"numIndexesBefore":2,"numIndexesAfter":3,"ok":1}

创建过期索引

> db.imooc_2.ensureIndex({time:1},{expireAfterSeconds:30}){"createdCollectionAutomatically":false,"numIndexesBefore":3,"numIndexesAfter":4,"ok":1}

相关文章

  • 24.Mongodb的索引操作

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

  • MongoDB 索引操作(2)

    前言 上一篇介绍了 MongoDB 的索引基本操作,包括了索引查看、创建、删除,具体可以参考:MongoDB 索引...

  • mongodb索引操作

    查看索引情况 db.imooc_collection.getIndexes() > show tables> db...

  • mongodb索引操作

    查看索引 创建复合索引:"0"字段升序“p”降序 删除索引

  • mongodb——内存

    sort操作内存溢出 报错 这个异常出自mongodb的sort操作。当mongodb无法从索引获取排序顺序时,会...

  • MongoDB 索引操作(1)

    在讲索引前先补一个小知识,即数据取样。 随机取样 可以实用$sample操作符从文档中取样,达到随机取样的效果。每...

  • MongoDB索引二(九)

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

  • mongodb基本操作

    本文主要介绍mongodb的一些基本操作,如创建、更新、查找、删除记录和创建索引。 1. 安装MongoDB 安装...

  • MongoDB学习报告(二)

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

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

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

网友评论

      本文标题:mongodb索引操作

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