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使用磁盘存储临时数据
相关推荐
子一!!几秒前
MySQL数据库基础操作
数据库·mysql·oracle
DarkAthena12 分钟前
【GaussDB】从 sqlplus 到 gsql:Shell 中执行 SQL 文件方案的迁移与改造
数据库·sql·oracle·gaussdb
Wpa.wk15 分钟前
接口自动化 - 了解接口自动化框架RESTAssured (Java版)
java·数据库·自动化
二等饼干~za89866820 分钟前
GEO优化---关键词搜索排名源码开发思路分享
大数据·前端·网络·数据库·django
程序员柒叔22 分钟前
Dify 集成-向量数据库
数据库·milvus·向量数据库·工作流·dify·向量库
月明长歌23 分钟前
MySQL 视图:把复杂查询封装成表,并且还能控权限、做解耦
数据库·mysql
l1t24 分钟前
postgresql 18版bytea 类型转换的改进
数据库·postgresql
小蒜学长27 分钟前
python餐厅点餐系统(代码+数据库+LW)
数据库·spring boot·后端·python
岳麓丹枫00131 分钟前
PostgreSQL 中 create database 中的注意事项
数据库·postgresql
梦想画家31 分钟前
告别关键词!PostgreSQL+pgvector 玩转语义和图像检索
数据库·postgresql