MongoDB查询的详细介绍

一.数据的基本查询:

        1.基本查询{

             1.查询所有的数据

                db.xx.find()

             2.指定条件查询

                db.xx.find(查询条件)

             3. 只查找符合条件的一个

            db.xx.findOne()

            db.stu.findOne({gender:true}).pretty()

        }

        2.条件运算 $lt $gt $lte $gte $ne

        {

            db.xx.find({字段:{修饰符:值}})

             // 取出年龄大于 20岁的人

            db.stu.find(

                {

                  age:{$gt:20}

                }

            )

             // 取出年龄不等于 20岁的

             db.stu.find({age:{$ne:20}})

             

            }

        3.逻辑运算 $and  $or

         db.xx.find(

             {

                 $and:[

                     {条件1},

                     {条件2}

                 ]

             }

         )

              

            1. 默认find的多条件 就是 $and

               //籍贯是 桃花岛 年龄是18岁的

               db.stu.find(

                   {

                    hometown:"桃花岛",

                    age:18

                   }

               )

               db.stu.find(

                   {

                       $and:[

                           {hometown:"桃花岛"},

                           {age:18}

                       ]

                   }

               )

              

            2. $or

               // 来自大理 或者是女生的

               db.stu.find(

                   {

                       $or:[

                           {hometown:"大理"},

                           {gender:false}

                       ]

                   }

               )

               

            3. 混合使用

               // 来自桃花岛或者年龄小于45岁 必须是男生

               db.stu.find(

                   {

                       $and:[

                           {

                               $or:[

                                   {hometown:"桃花岛"},

                                   {age:{$lt:45}}

                               ]

                           },

                           {gender:true}

                       ]

                   }

               )

               // 来自大理或者来自蒙古   必须男的 年龄大于16

               db.stu.find(

                   {

                       $or:[

                           {hometown:{$in:['大理','蒙古']}},

                           {

                               $and:[

                                   {gender:true},

                                   {age:{$gt:16}}

                               ]

                           }

                       ]

                   }

               )

            }

        4.范围运算符 $in  $nin

            {

               // 不是 桃花岛

               db.stu.find(

                {hometown:{$nin:["桃花岛"]}}

            )

               

               // 取 年龄 18 40  45

               db.stu.find(

                   {age:{$in:[18,40,45]}}

               )

            }

               

          5.正则表达式{

               db.xx.find(

                   {字段:'正则表达式'}

               )

               1. /正则/

               db.stu.find(

                   {

                       name:/黄/

                   }

               )

               2.{$regex:"正则"}

               db.stu.find(

                   {

                       name:{$regex:"段"}

                   }

               )

             

               3. 忽略大小写

               3.1 db.stu.find({name:/lily/i})

               3.2 db.stu.find(

                   {

                       name:{$regex:"lcf",

                            $options:"i"}

                   }

               )

            }

        6.自定义函数  $where

         db.xx.find(

             {

             $where:带返回值的 匿名函数

             }

         )

            //取年龄 大于20岁的人  age > 20

            db.stu.find(

                {

                    $where:function () {

                        return  this.age > 20

                    }

                }

            )

            

二.查询结果显示

       1.skip 跳过几个显示

       2.limit 允许显示几个

            //跳过两个 显示4个

            db.stu.find().skip(2).limit(4)

            db.stu.find().limit(4).skip(2)

            

            注意点:  没有顺序之分

       3.投影 显示哪些字段; 显示 true  不显示false

            db.xx.find(

                {查询条件},

                {字段:true}

            )

            db.stu.find(

                {},

                {name:true}

            )

            

       4.sort 排序:  -1降序 1 升序

            // 年龄 排降序, 相同的按照id 降序

            db.stu.find({}).sort({age:-1,_id:-1})

       5.count 统计个数

         //统计所有

          db.xx.count()

        //   根据条件

        db.xx.find().count()

         6.distinct 去重

           // 取出年龄 小于 60岁的; 籍贯

          db.xx.distinct("去重字段",{查询条件})

          db.stu.distinct("hometown",{age:{$lt:60}})

三.聚合查询:

        db.xx.aggreagate(

            [

                {管道1},

                {管道2},

                {管道3}

            ]

        )           

            // 多条件的 多件事

                

           1. $group: 分组

                例子:  数据按照 gender性别分组

                db.stu.aggregate(

                    

                        {$group:{_id:"$gender"}}

                    

                )

               

                注意:  表达式: $sum $avg $first $last $max $min  $push

                例子: 数据按照 性别分组 求年龄的平均值

               db.stu.aggregate(

                   {$group:{_id:"$gender",age_avg:{$avg:"$age"}}}

               )

                例子: 按照籍贯分组, 求年龄和

                db.stu.aggregate(

                    {$group:{_id:"$hometown",age_sum:{$sum:"$age"}}}

                )

                

                注意: $push  将分组想取出来的数据 放入到一个列表

                例子:  取出 按照性别分组的 所有人的名字

                db.stu.aggregate(

                    {$group:{_id:"$gender",all_name:{$push:"$name"}}}

                )

               

              2.$match: 查找符合条件的数据 ; find 区别

                    查询功能一样

                    match可以配合管道使用的

                   例子: 取出年龄大于20人

                   db.stu.find({age:{$gt:20}})

                   db.stu.aggregate(

                        {$match:{age:{$gt:20}}}

                   )

                   

                    例子: 取出年龄小于40的; 按照性别分组 求年龄平均值($avg)

                   db.stu.aggregate(

                       {$match:{age:{$lt:40}}},

                       {$group:{_id:"$gender",age_avg:{$avg:"$age"}}}

                   )

                   

             3. $project:投影取出部分字段; 显示1  

                   例子: 取出年龄大于20; 按照籍贯分组 求出年龄之和,求年龄平均值; 查看的时候只想看到之和

                   db.stu.aggregate(

                       {$match:{age:{$gt:20}}},

                       {$group:{_id:"$hometown", age_sum:{$sum:"$age"},age_avg:{$avg:"$age"}}},

                       {$project:{age_sum:1,age_avg:1}}

                   )

                   

             4.$sort: 排序 1升序 -1降序

                   例子: 先找 45以下 ;再 安籍贯分组 求平均值, 在 降序, 在投影

                   db.stu.aggregate(

                       {$match:{age:{$lt:45}}},

                       {$group:{_id:"$hometown", age_avg:{$avg:"$age"}}},

                       {$sort:{age_avg:-1}},

                       {$project:{_id:0}}

                   )

                   

             //注意点: 管道是有顺序的 不能随意颠倒; 根据需求

           5. $skip  跳过几个查看

            $limit 允许显示几个

                   例子: 先跳2个 在显示 4个

                   db.stu.aggregate(

                       {$match:{age:{$lt:60}}},

                       {$skip:2},

                       {$limit:4}

                   )

                   db.stu.aggregate(

                    {$match:{age:{$lt:60}}},

                    {$limit:4},

                    {$skip:2}

                )

                   

            6. $unwind :将数据列表 分割  拆分

                  例子:按照性别分组, 求出人的名字 $push

               db.stu.aggregate(

                   {$group:{_id:"$gender", all_name:{$push:"$name"}}},

                   {$unwind:"$all_name"}

               )

猜你喜欢

转载自blog.csdn.net/Dr_xin_918/article/details/81114234