- 创建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
查询
db.myCollection.aggregate([
{
$group: {
_id: "$groupId", // 根据groupId分组
count: { $sum: 1 }, // 分组内文档数量累加,并存储到count字段中
documents: { $push: "$$ROOT" } // 将原始文档存储到 documents 数组中
}
},
{
$match: {
count: { $gt: 2 } // 过滤出count字段值大于2的分组
}
}
]);
// $group聚合操作默认内存中执行,超出限制可配置allowDiskUse使用磁盘存储临时数据