zoukankan      html  css  js  c++  java
  • MongoDB数据库常用SQL命令 — MongoDB可视化工具Robo 3T

    1、db.collection.updateMany() 修改集合中的多个文档。

    db.getCollection('user').find({"pId":"3332a512df604a74a72f267cf246"}).updateMany({"pId":"c8018dd802a644a19517790336f"})

    2、模糊查询

    db.getCollection('user').find({name:{$regex:"AA"}})

    db.getCollection('user').find({name:/AA/})

    3、查询name是否为AA,BB,CC,DD的记录

    db.getCollection('user').find({"name":{$in:["AA","BB","CC","DD"]}})  //属于 - in

    db.getCollection('user').find({"name":{$nin:["AA","BB","CC","DD"]}})  //不属于 - nin

    4、按照时间排序

    db.getCollection('user').find({name:{$regex:"AA"}}).sort({ lastUpdatedTime : 1 }) //时间正序

    db.getCollection('user').find({name:{$regex:"AA"}}).sort({ lastUpdatedTime : -1 }) //时间倒序

    5、字段是否存在

    db.getCollection('user').find({age:{$exists:true}}) 

    6、对数组中的某一个元素进行查询

    db.getCollection('template').find({"content.pages.questions.type":"A"})

    7、limit() 和skip() 方法操作

    使用limit() 方法来读取指定数量的数据,limit方法接受一个数字参数作为读取的记录条数

    使用skip() 方法来跳过指定数量的数据,skip方法接受一个数字参数作为跳过的记录条数

    db.getCollection('user').find({}).limit(5).skip(1) — 跳过第1条,展示第2条到第6条

    8、时间范围查询

    greater than(大于)

    less than(小于)

    (>) 大于 - $gt

    (<) 小于 - $lt

    (>=) 大于等于 - $gte

    (<= ) 小于等于 - $lte

    db.getCollection('student').find({"createdTime":{$lt:new Date(2019,8,16)}})  //创建时间在2019.8.16之前的记录

    db.getCollection('student').find({"createdTime":{$lte:new Date(2019,8,31),$gte:new Date(2019,3,1)}})  //创建时间在2019.3.1到2019.8.31之间的记录

    9、更新语句,如果你要修改多条相同的文档,则需要设置 multi 参数为 true

    db.getCollection('user').update({"name":"张三"},{$set:{"name":"李四"}},{multi:true})

  • 相关阅读:
    maven报错【Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of】
    Srping框架中使用@query注解实现复杂查询
    FreeMarker自定义TemplateDirectiveModel
    SpringMVC和Freemarker整合,带自定义标签的使用方法
    关于FreeMarker自定义TemplateDirectiveModel
    滑块验证码【插件待研究】
    注册页面 注册码【欠缺较多 待完善】
    IO流-文件的写入和读取
    Date、String、Calendar相互转化
    Runtime类
  • 原文地址:https://www.cnblogs.com/wueryuan/p/11505244.html
Copyright © 2011-2022 走看看