MongoDB#常用语句

  • 创建TTL索引(自动删除过期数据)

db.xxx_collection.createIndex({ createTime: 1 }, { expireAfterSeconds: 1 * 24 * 60 * 60 * 1000 });

  • 查询JavaScript函数(mongosh)

db.system.js.find

  • 查询document条数

db.getCollection('xxx').countDocuments({})

  • 根据_id查询

{'_id': ObjectId('xxx')}

  • in查询

{ field_name: { $in: [ "field_value1", "field_value2" ] } }

  • 时间范围查询

{"updateTime": {gte: ISODate('2024-01-01'), lte: ISODate('2024-12-12')}}

  • like查询

{ field_name: { $regex: /xxx/ } }

  • aggregate查询
sql 复制代码
db.myCollection.aggregate([
  {
    $group: {
      _id: "$groupId", // 根据groupId分组
      count: { $sum: 1 }, // 分组内文档数量累加,并存储到count字段中
      documents: { $push: "$$ROOT" }  // 将原始文档存储到 documents 数组中
    }
  },
  {
    $match: {
      count: { $gt: 2 } // 过滤出count字段值大于2的分组
    }
  }
]);
// $group聚合操作默认内存中执行,超出限制可配置allowDiskUse使用磁盘存储临时数据
相关推荐
嘟嘟w11 分钟前
DROP DELETE 和TRUNCATE的区别?
数据库·mysql·oracle
Navicat中国24 分钟前
Navicat x 达梦技术指引 | 数据字典
数据库·达梦·可视化·navicat·数据字典
running up1 小时前
Spring核心深度解析:AOP与事务管理(TX)全指南
java·数据库·spring
一水鉴天1 小时前
整体设计 定稿 之6 完整设计文档讨论及定稿 之1(豆包周助手)
java·前端·数据库
倔强的石头_1 小时前
金仓数据库(KingbaseES) 开发实战:常见迁移挑战与技术解析
数据库
编程小白gogogo1 小时前
苍穹外卖后端环境搭建-数据库环境搭建
数据库
星哥说事2 小时前
基于 MySQL Monitoring and Management 的性能保障实践
数据库·mysql
此生只爱蛋2 小时前
【Redis】列表List类型
数据库·redis·缓存
Jaising6662 小时前
Mybatis Plus 主键生成器实现思路分析
数据库·spring boot·mybatis
程序喵大人2 小时前
SQLITE问题整理
开发语言·数据库·c++·sqlite