MongoDB查询的详细介绍

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: MongoDB查询的详细介绍 一.数据的基本查询:         1.基本查询{              1.查询所有的数据                 db.xx.find()              2.

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"}

               )

相关文章
|
8月前
|
NoSQL 测试技术 MongoDB
微服务——MongoDB实战演练——根据上级ID查询文章评论的分页列表
本节介绍如何根据上级ID查询文章评论的分页列表,主要包括以下内容:(1)在CommentRepository中新增`findByParentid`方法,用于按父ID查询子评论分页列表;(2)在CommentService中新增`findCommentListPageByParentid`方法,封装分页逻辑;(3)提供JUnit测试用例,验证功能正确性;(4)使用Compass插入测试数据并执行测试,展示查询结果。通过这些步骤,实现对评论的高效分页查询。
144 0
|
3月前
|
存储 JSON NoSQL
查询 MongoDB--SPL 轻量级多源混算实践 4
SPL 支持多种数据源连接,包括 MongoDB 等 NoSQL 数据库。通过外部库形式提供驱动,灵活扩展,可实现实时数据计算与混合分析。
|
存储 NoSQL MongoDB
掌握MongoDB索引优化策略:提升查询效率的关键
在数据库性能调优中,索引是提升查询效率的利器。本文将带你深入了解MongoDB索引的内部工作原理,探讨索引对查询性能的影响,并通过实际案例指导如何针对不同的查询模式建立有效的索引。不仅将涵盖单一字段索引,还会探讨复合索引的使用,以及如何通过分析查询模式和执行计划来优化索引,最终实现查询性能的最大化。
|
10月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过MongoDB Atlas Data Lake或Apache Drill,可以在Java中使用SQL语法查询MongoDB数据。这两种方法都需要适当的配置和依赖库的支持。希望本文提供的示例和说明能够帮助开发者实现这一目标。
412 17
|
11月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过使用 MongoDB Connector for BI 和 JDBC,开发者可以在 Java 中使用 SQL 语法查询 MongoDB 数据库。这种方法对于熟悉 SQL 的团队非常有帮助,能够快速实现对 MongoDB 数据的操作。同时,也需要注意到这种方法的性能和功能限制,根据具体应用场景进行选择和优化。
435 9
|
NoSQL 定位技术 MongoDB
解锁MongoDB索引的秘密:优化查询效率与应对限制的策略
解锁MongoDB索引的秘密:优化查询效率与应对限制的策略
277 0
|
存储 NoSQL MongoDB
MongoDB 查询分析
10月更文挑战第21天
113 1
|
NoSQL MongoDB 索引
MongoDB 覆盖索引查询
10月更文挑战第21天
122 1
|
SQL NoSQL MongoDB
MongoDB 查询文档
10月更文挑战第15天
423 1
|
人工智能 NoSQL 机器人
MongoDB Atlas与YoMio.AI近乎完美适配:推理更快速、查询更灵活、场景更丰富
随着MongoDB的新发布和革新,YoMio.AI的“闪电式发展”值得期待。

推荐镜像

更多
下一篇
oss云网关配置