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使用磁盘存储临时数据
相关推荐
CopyLower几秒前
MySQL 5.7 之后的特性解析:从 8.0 到 8.4 的技术进化
数据库·mysql
chat2tomorrow43 分钟前
数据中台建设系列(五):SQL2API驱动的数据共享与服务化实践
大数据·数据库·数据仓库·sql·数据治理·数据中台·sql2api
极小狐1 小时前
如何使用极狐GitLab 软件包仓库功能托管 helm chart?
java·linux·服务器·数据库·c#·gitlab·maven
JavaAlpha1 小时前
高频面试题:设计秒杀系统,用Redis+Lua解决超卖
数据库·redis·lua
向上的车轮2 小时前
什么是向量数据库?向量数据库和关系数据库有什么区别?
数据库·向量数据库
boring_1112 小时前
异地多活单元化架构下的微服务体系
数据库·微服务·架构
betazhou2 小时前
oracle goldengate非并行进程转换为并行进程
数据库·oracle·并行·parallel·ogg·同步数据
wuli玉shell2 小时前
数仓-范式建模、维度建模、雪花模型、星型模型对比及其适用范围
数据库·oracle
ghie90902 小时前
oracle dblink varchar类型查询报错记录
数据库·oracle
等rain亭2 小时前
MySQL数据库创建、删除、修改
数据库·mysql