zoukankan      html  css  js  c++  java
  • MongoDB学习笔记<六>

    继续mongoDB的学习

    --索引具体解释

    --索引管理

    --空间索引

    1.创建简单索引

    (1)先准备20万条数据

    for(var i = 0;i< 200000;i++){

       db.books.insert("number":i,"name":i+"book")

    }

    (2)检查一下查询性能

    var start = new Date()

    db.books.find({"number":123456})

    var end = new Date();

    end - start

    (3)为number创建索引

    db.books.ensureIndex({"number":1}) 此处1代表正序,-1代表倒序

    2.须要注意的地方

    -索引的创建在提升查询性能的同一时候会影响插入的性能

    -对于常常查询少插入的文档能够考虑使用索引

    -每一个键都建立索引不一定能够提高性能

    -在做排序工作时,假设是大数据量也能够考虑索引

    3.创建索引时能够同一时候指定索引的名字

    db.books.ensureIndex({"name":1},{name:"bookname"})

    4.唯一索引

    怎样解决集合books不能插入反复的文档

     建立唯一索引:db.books.ensureIndex({"name":1},{unique:true})

    5.剔除反复值

    假设建立唯一索引之前,已经有反复文档,怎么办?

    db.books.ensureIndex({"name":1},{unique:true,dropDups:true})

    6.hint

    怎样强制查询使用指定的索引

    db.books.find({"name":"obook"}).hint({"name":1})

    指定索引必须是已经创建好了的索引

    7.explain

    怎样具体的查看本次查询使用哪个索引和查询数据的状态信息

    db.books.find({"name":"0book"}).explain()

    8.索引的创建过程在后台运行

    db.books.ensureIndex({"name":"0book"},{bookground:true})

    9.删除索引

    db.runCommand({dropIndexes:"books",index:"name_1"})精确删除

    db.runCommand({dropIndexes:"books",index:"*"})全部都删除




  • 相关阅读:
    asp.net mvc学习(Vs技巧与Httpcontext)
    微信小程序代码构成
    MetaEditor中MQL使用方法
    lambda表达式中的排序问题
    Python Bs4 回顾
    Python操作Redis及连接方式
    C# for Python(Nugut Iron包)
    Jquery.ajax dataType参数
    Fleck For Web Socket
    visual studio git for coding
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6788985.html
Copyright © 2011-2022 走看看