MongoDB常用语句

1.只统计记录总数:

复制代码
let result = await CorrectionRecordModel.countDocuments(db);

2.数组遍历,循环体中可以有调用异步函数:

复制代码
            for(let item of result2){
                if(item && Tool.checkNotEmptString(item.auth_id) && (item.status !== 1)){
                    await apiUtil.modifyData(EstateDetailModel, {auth_id: item.auth_id}, {
                        status: 1, 
                    });
                }
            }

3.遍历数组,循环体中不可以调用异步函数:

复制代码
                space_service_labels.forEach(item =>{
                    let name = Tool.getEnumValue(14, item);
                    if(Tool.checkNotEmptString(name)){
                        space_service_labels_names.push(name);
                    }
                });

4.对数组,投影出指定对象组成新数组:

复制代码
let ids = list.map(item => item.id);

5.查询出指定距离内的记录:

复制代码
                    db.location = {
                        $near: {
                            $geometry: currentLocation,
                            $maxDistance: distance1
                        }
                    };

6.对查询出的结果数组,按照指定字段再排序:

复制代码
// 按照每组内的记录数降序排序
                groupedArray.sort((a, b) => b.count - a.count);

7.数据查询条件中对结果进行排序(-1从倒序,1正序):

复制代码
db.sort({ update_time : -1 })

8.聚合语句:

复制代码
        let result = await ApiUserPushModel.aggregate([
            {
                $match: {
                    user_id:,
                    create_time: {
                        $gte: start_time,
                        $lte: end_time
                    },
                }
            },
            {
                $group: {
                    _id: '$batch_number',
                    push_count: { $sum: 1 },
                    push_title: { $first: '$push_title' },
                    commission : { $first: '$commission' },
                    notes : { $first: '$notes' },
                    create_time : { $first: '$create_time' },
                    batch_number : { $first: '$batch_number' },
                    data: { $push: '$$ROOT' },
                    
                }
            },
            {
                $project:{
                    _id:0
                }
            }
            
        ]);
  1. 查询是否存在:

    复制代码
             const exist = await UserModel.exists({
                 user_name: user_name
             });
相关推荐
gaoshou4539 分钟前
代码随想录训练营第三十一天|LeetCode56.合并区间、LeetCode738.单调递增的数字
数据结构·算法
自信的小螺丝钉1 小时前
Leetcode 240. 搜索二维矩阵 II 矩阵 / 二分
算法·leetcode·矩阵
li35741 小时前
从“内存操作”到“原子更新”:一次代码思维的跃迁
数据库·oracle
瀚高PG实验室1 小时前
执行select * from a where rownum<1;,数据库子进程崩溃,业务中断。
数据库·sql·瀚高数据库
小白考证进阶中1 小时前
终于赶在考试券过期前把Oracle OCP证书考下来了!
运维·数据库·oracle·dba·开闭原则·数据库管理员
DCTANT2 小时前
【报错记录】OpenGauss/磐维数据库连接报:org.postgresql.util.PSQLException: 致命错误: 账户被锁定
数据库·postgresql
KING BOB!!!2 小时前
Leetcode高频 SQL 50 题(基础版)题目记录
sql·mysql·算法·leetcode
keep__go2 小时前
postgresql9.2.4 跨版本升级14.6
linux·运维·数据库·postgresql
Doris_LMS2 小时前
Git的强软硬回退(三)
运维·服务器·数据库·git·idea
我是渣哥3 小时前
Java String vs StringBuilder vs StringBuffer:一个性能优化的探险故事
java·开发语言·jvm·后端·算法·职场和发展·性能优化