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使用磁盘存储临时数据
相关推荐
zzh940772 分钟前
MySQL中的通配符
数据库·mysql
gameboy0312 分钟前
MySQL:基础操作(增删查改)
数据库·mysql·oracle
yoyo_zzm6 分钟前
MySQL的索引
android·数据库·mysql
未来龙皇小蓝8 分钟前
【MySQL-索引调优】06:最左匹配原则及优化
数据库·mysql·oracle·性能优化
一个有温度的技术博主31 分钟前
Redis系列三:在linux上安装Redis
linux·数据库·redis
changhong198631 分钟前
redis批量删除namespace下的数据
数据库·redis·缓存
IvorySQL34 分钟前
PostgreSQL 技术日报 (3月18日)|从 MD5 到 SCRAM:PG 的安全转变
数据库·postgresql·开源
代码派1 小时前
MySQL 慢 SQL 排查这件事,NineData 社区 VS DBeaver/ Navicat 技术分析
数据库·sql·mysql·navicat·数据库管理工具·dbeaver·数据库对比
白菜!!!1 小时前
SQL中IF、IFNULL、NULLIF、ISNULL函数的使用
数据库·sql·mysql
H_老邪2 小时前
redis 安装
数据库·redis·缓存