美文网首页
mongodb find aggregate管道 常用查询

mongodb find aggregate管道 常用查询

作者: xiuxiuxiucai | 来源:发表于2019-09-30 17:47 被阅读0次

    find查询(此例基本包含了mongodb find查询所有的常用操作):

    db.test.find(
        {
            // 范围 $gte $lte
            hours: {$gte: "2019-01-01 00", $lt: "2019-01-01 01"}, 
            // 多条件查询 并 $and
            $and: [
                {
                    // 模糊查询
                    code: /^.*02/i
                }, 
                {
                    // 多条件查询 或 $or
                    $or: [
                        {
                            // 不包含此条件 $not,使用正则表达式 $regex
                            code: {$not: {$regex: "air_.*"}}
                        }, 
                        {
                            // 多值查询 $in
                            code: {$in: ["1718", "1719", "1720"]}
                        }
                    ]
                }
            ], 
            // 如果同一字段未使用 $and $or 的情况下多条件查询,后一个条件会顶替掉前一个条件
            code: "air_130", 
            //不等于 $ne
            code: {$ne: "air_130"}
        }, 
        // 规定输出字段(find无法起别名,需用管道 aggregate)
        {
            code: 1, 
            hours: 1
        }
        // 在mongo中多条件的最后,多一个逗号不会报错
        ,
    )
    // 排序 sort(-1为倒序)
    .sort(
        {
            hours: -1,
            code: -1
        }
    )
    // 查询条数 limit,跳过条数 skip,结果为倒数第三至十二条数据
    .limit(10).skip(2)
    // 计数(先limit再计数会冲突)
    .count()
    

    aggregate管道查询(此例基本包含了mongodb管道查询所有的常用操作):

    db.test.aggregate([
        {
            // 查询条件
            $match: {
                // 条件1
                $or: [
                    {hours: {$gte: "2019-09-01 00", $lte: "2019-09-01 00"}},
                    {hours: {$gte: "2019-09-03 00", $lte: "2019-09-03 02"}}
                ],
                // 条件2
                code: {$regex: ".*air_15.*"}
            }
        },
        {
            // 分组
            $group: {
                // 多字段分组,_id后是分组字段,分组字段可写为null来将所有结果聚合到一起
                _id: {hours: "$hours", time: "$time"},
                
                // 取第一个值
                first_V_17: {$first: "$V_17"},
                // 取最后一个值,作用于多个字段
                last_data: {$last: {V_14: "$V_14",V_12: "$V_12"}},
                
                // 取最大的一个值
                max_V_16: {$max: "$V_16"},
                // 取最小的一个值
                min_V_12: {$min: "$V_12"},
                
                // 取所有值组成数组,允许重复
                any_V_18: {$push: "$V_18"},
                // 取所有值组成数组,去除重复值
                alone_V_18: {$addToSet: "$V_18"},
                
                // 取平均值
                avg_V_11: {$avg: "$V_11"},
                // 求和
                sum_V_11: {$sum: "$V_11"},
                // 计数
                number: {$sum: 1},
            }
        },
        {
            // 查询条件,对前面的查询结果再次过滤
            $match: {
                number: 7
            }
        },
        {
            // 排序
            $sort: {
                // 使用内嵌文档特定键值排序,需加引号
                "_id.hours": 1
            }
        },
        {
            // 规定输出字段
            $project: {
                // 别名
                hours: "$_id.hours",
                time: "$_id.time",
                
                // 无别名
                first_V_17: 1,
                last_data: 1,
                max_V_16: 1,
                min_V_12: 1,
                
                any_V_18: 1,
                alone_V_18: 1,
                
                avg_V_11: 1,
                sum_V_11: 1,
                number: 1,
            }
        },
        {
            // 指定为0,即去除指定输出字段,不可与规定输出字段同一对象内使用
            $project: {
                _id: 0,
                time: 0
            }
        },
        {
            // 查询条数
            $limit: 4
        },
        {
            // 跳过条数,管道中limit和skip的先后顺序会影响最后的输出条数,当前结果为3条
            $skip: 1
        },
        {
            // 计数
            $count: "count"
        }
    ])
    

    相关文章

      网友评论

          本文标题:mongodb find aggregate管道 常用查询

          本文链接:https://www.haomeiwen.com/subject/zqlhpctx.html