MongoDB常用的语句

mongodb 可视化工具:

Robo3T。

DataGrip,对mongodb的日期格式不友好。

MongoDB常用的语句:

注意,凡是涉及到日期类型的,最好都用这种格式 ISODate("2023-03-27T16:00:00.000Z")

MongoDB倒序,查询:

复制代码
db.getCollection('abcde').find().sort({ 'createTime': -1 })

MongoDB查询,限制个数:

复制代码
db.getCollection('abcdef').find().sort({ 'createTime': -1 }).limit(10)

MongoDB插入数据

MongoDB不需要建表,直接插入数据就会建表。

日期用 ISODate() 转换。

复制代码
db.getCollection("mongoDbTest").insert({userId:"dxcefg", status:1,  price:1.23, updateTime : ISODate("2022-02-13T07:06:25.371Z")})

MongoDB查询全部

MongoDB的 Collection(集合),类似于 数据库的表。

复制代码
db.getCollection("mongoDbTest").find()

MongoDB条件查询

复制代码
db.getCollection("mongoDbTest").find({userId:"abc", status: 1})

MongoDB模糊查询

复制代码
//以abc开头
db.getCollection("mongoDbTest").find({userId: /^abc/})
//以abc结尾
db.getCollection("mongoDbTest").find({userId: /efg$/})

MongoDB查询数量

复制代码
db.getCollection("mongoDbTest").find({userId: /^abc/}).count()

mongoDB查询日期:

lte 小于等于,gte 大于等于

复制代码
db.getCollection('mongoDbTest').find({
   "startTime" : { "$lte" :  ISODate("2023-03-27T16:00:00.000Z")  },
  "endTime" : { "$gte" : ISODate("2023-03-27T16:00:00.000Z")  }
})

MongoDB新增字段

比如新增一个字段叫 fieldTest,如下 :

复制代码
db.getCollection("mongoDbTest").update({},{$set:{ fieldTest:""}})

MongoDB修改字段名

复制代码
//参数提示:
//第一个false:可选,这个参数的意思是,如果不存在update的记录,true为插入新的记录,默认是false,不插入。
//第二个true:可选,mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
db.getCollection("mongoDbTest").update({}, {$rename : {"orderId" : "status"}}, false, true)

MongoDB删除字段

复制代码
db.getCollection("mongoDbTest").update({
    "fieldTest": {
        "$exists": true
    }
}, {
    "$unset": {
        "fieldTest":null
    }
})

MongoDB修改字段值

update第一个参数是 条件,而 set部分就是修改内容

以下类似于: update mongoDbTest set status= 4 where status= 1;

复制代码
db.getCollection("mongoDbTest").update(
{"status": 1},
{$set:    { "status" : 4 } })

MongoDB删除:

复制代码
db.getCollection("mongoDbTest").deleteMany({ id:1 })

MongoDb 判断数组非空

复制代码
db.getCollection('MongoDbTest').find({ "$or": [
    {
        "数组字段名称": {
            "$eq": []
        }
    },
    {
        "数组字段名称": {
            "$eq": null
        }
    }
 ]})

MongoDB添加索引/查询索引:

复制代码
//查索引
db.getCollection('表名').getIndexes();

//加索引
db.getCollection('表名').createIndex({"personList":1})

//给Json的某个字段加索引
db.getCollection('表名').createIndex({"personList.personId":1})

MongoDB查看执行计划

复制代码
//查看执行计划
db.getCollection('表名').find({'id':'abcd'}).explain()
//explain()得到的执行计划字段:
//stage	查询方式,常见的有COLLSCAN/全表扫描、IXSCAN/索引扫描、FETCH/根据索引去检索文档、SHARD_MERGE/合并分片结果、IDHACK/针对_id进行查询
//mongoDB的执行计划字段,详情见: https://www.uoften.com/article/221616.html

打印mongoDB语句的日志关键词:

使用 mongoTemplate 查询。

搜索关键词: find using query

SpringBoot配置mongodb打印日志。详情见:https://www.cnblogs.com/expiator/p/17375443.html

参考资料:

https://www.uoften.com/article/221616.html

相关推荐
Navicat中国41 分钟前
如何使用 Ollama 配置 AI 助手 | Navicat 教程
数据库·人工智能·ai·navicat·ollama
小猿姐5 小时前
实测对比:哪款开源 Kubernetes MySQL Operator 最值得用?(2026 深度评测)
数据库·mysql·云原生
倔强的石头_7 小时前
从 “存得下” 到 “算得快”:工业物联网需要新一代时序数据平台
数据库
TDengine (老段)8 小时前
TDengine IDMP 可视化 —— 分享
大数据·数据库·人工智能·时序数据库·tdengine·涛思数据·时序数据
GottdesKrieges9 小时前
OceanBase数据库备份配置
数据库·oceanbase
SPC的存折9 小时前
MySQL 8组复制完全指南
linux·运维·服务器·数据库·mysql
运维行者_9 小时前
OpManager MSP NetFlow Analyzer集成解决方案,应对多客户端网络流量监控挑战
大数据·运维·服务器·网络·数据库·自动化·运维开发
炸炸鱼.11 小时前
Python 操作 MySQL 数据库
android·数据库·python·adb
softshow102611 小时前
Etsy 把 1000 个 MySQL 分片迁进 Vitess
数据库·mysql
Ronaldinho Gaúch11 小时前
MySQL基础
数据库·mysql