zoukankan      html  css  js  c++  java
  • mongodb 学习笔记 04 -- 游标、索引

    游标

    • var cursor = db.collectionName.find() 创建游标
    • cursor.hasNext() 是否有下一个元素
    • cursor.next() 取出下一个元素 比如 while(cursor.hasNext()) { printjson(cursor.next()); }
    • cursor.forEach(function) 循环 比如cursor.forEach(function (obj) {printjson(obj);})

    • 实现分页
      cursor.skip(n) 跳过n行
      cursor.limit(n) 显示n行
      比如:显示第5页。一页10条
      var cursor = db.stu.find().skip(5*9).limit(10);

    • 转化为数组
      cursor.toArray()

    索引

    • cursor.explain() 查看查询计划
    • db.collectionName.ensureIndex({xxx}) 创建单列索引 比如db.stu.ensureIndex({age:1}) 对age升序
    • db.collectionName..getIndexes() 查看索引
    • db.collectionName.dropIndex() 删除全部索引
    • db.collectionName.dropIndex({xxx}) 删除索引
    • db.collectionName.reIndex() 重建索引

    • 创建多列索引
      db.stu.ensureIndex({age:1,stu_id:-1})

    • 创建子文档索引
      db.stu.ensureIndex({father.age:1})

    • 创建唯一索引
      db.stu.ensureIndex({stu_id:1},{unique:true})

    • 创建哈希索引
      db.stu.ensureIndex({name:’hashed’})

  • 相关阅读:
    MySQL高级查询总结
    MySQL数据库作业
    MySQLdump备份还原命令
    MySQL之Join
    MySQL课堂作业(一)
    Mysql数据库
    Js实例之简易计算器
    JS系统函数
    js课堂作业之转换月份
    C++ Name Mangling 为什么不编码返回值参数
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5225286.html
Copyright © 2011-2022 走看看