Mongodb从0到1系列三: 条件查询、大小写

时间:2021-03-09 00:49:54

Mongodb从0到1系列一:下载、安装、启动、停止

Mongodb从0到1系列二:数据库与集合操作文档、增删改查


5. 条件查询

上一节中提到了使用find()方法来查询文档,本节更加详细地介绍如何使用这个命令进行条件查询。

find()方法语法如下:

db.collection.find(query, projection)
query :可选,使用查询操作符指定查询条件
projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)

先往student集合里插入一些文档:
MongoDB Enterprise > use test1
MongoDB Enterprise > db.student.insert({name:'Zhangsan',age:22,course:'Chinese'})
MongoDB Enterprise > db.student.insert({name:'Lisi',age:23,course:'computer'})
MongoDB Enterprise > db.student.insert({name:'Wangwu',age:24,course:'Chinese'})
MongoDB Enterprise > db.student.insert({name:'Zhaoliu',age:25,course:'Chinese'})
MongoDB Enterprise > db.student.insert({name:'Liuneng',age:26,course:'English'})

5.1 比较条件

Mongodb从0到1系列三: 条件查询、大小写


示例1:查询年龄等于24的文档:
MongoDB Enterprise > db.student.find({age:24}).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf751420"),
        "name" : "Wangwu",
        "age" : 24,
        "course" : "Chinese"
}

示例2: 查询年龄小于24的文档
MongoDB Enterprise > db.student.find({age:{$lt:24}}).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf75141e"),
        "name" : "Zhangsan",
        "age" : 22,
        "course" : "Chinese"
}
{
        "_id" : ObjectId("59703a654c9b439bdf75141f"),
        "name" : "Lisi",
        "age" : 23,
        "course" : "computer"
}

5.2 and 条件

语法格式如下:
>db.collection.find(
   {
      $and: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

示例3: 查询年龄小于24的,并且选课是computer的文档
MongoDB Enterprise > db.student.find( { $and:[{age:{$lt:24}}, {course:'computer'}] } ).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf75141f"),
        "name" : "Lisi",
        "age" : 23,
        "course" : "computer"
}

其实,$and:[]是可以省略的,多个条件中间以逗号隔开即可,上面的查询可以简写如下:
MongoDB Enterprise > db.student.find( {age:{$lt:24}, course:'computer'}).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf75141f"),
        "name" : "Lisi",
        "age" : 23,
        "course" : "computer"
}

示例4:查询满足三个条件的文档:
MongoDB Enterprise > db.student.find( {age:{$gt:23}, course:'Chinese', name:'Zhaoliu'}).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf751421"),
        "name" : "Zhaoliu",
        "age" : 25,
        "course" : "Chinese"
}

5.3 or 条件

OR条件语法如下

>db.collection.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

示例5:查询年龄小于24的,或者选课是English的文档
MongoDB Enterprise > db.student.find( { $or:[ { age:{$lt:24} }, {course:'English'} ] } ).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf75141e"),
        "name" : "Zhangsan",
        "age" : 22,
        "course" : "Chinese"
}
{
        "_id" : ObjectId("59703a654c9b439bdf75141f"),
        "name" : "Lisi",
        "age" : 23,
        "course" : "computer"
}
{
        "_id" : ObjectId("59703a674c9b439bdf751422"),
        "name" : "Liuneng",
        "age" : 26,
        "course" : "English"
}

示例6:查询年龄小于24的,并且选课是computer的文档;或者选课是English的文档
MongoDB Enterprise > db.student.find( { 
...   $or:[  
...         { age:{$lt:24} , course:'computer' },  
...         { course:'English'} 
...      ] 
... }
... ).pretty()
{
        "_id" : ObjectId("59703a654c9b439bdf75141f"),
        "name" : "Lisi",
        "age" : 23,
        "course" : "computer"
}
{
        "_id" : ObjectId("59703a674c9b439bdf751422"),
        "name" : "Liuneng",
        "age" : 26,
        "course" : "English"
}

5.4 查询固定列

在find()第二个参数中,指定键名且值为1或者true则是查询结果中显示的键;若值为0或者false,则为不显示键。文档中的键若在参数中没有指定,查询结果中将不会显示(_id例外)。这样我们就可以灵活显示声明来指定返回的键。

示例7:只查询name键和age键:
MongoDB Enterprise > db.student.find({},{"name":1,"age":1,"_id":0})
{ "name" : "Zhangsan", "age" : 22 }
{ "name" : "Lisi", "age" : 23 }
{ "name" : "Wangwu", "age" : 24 }
{ "name" : "Zhaoliu", "age" : 25 }
{ "name" : "Liuneng", "age" : 26 }

若文档中不存在指定的key,则返回空:
MongoDB Enterprise > db.student.insert({name:'Liuneng222',age2:32,course:'English'})
MongoDB Enterprise > db.student.insert({name2:'Liuneng222',age2:32,course:'English'})
MongoDB Enterprise > db.student.find({},{"name":1,"age":1,"_id":0})
{ "name" : "Zhangsan", "age" : 22 }
{ "name" : "Lisi", "age" : 23 }
{ "name" : "Wangwu", "age" : 24 }
{ "name" : "Zhaoliu", "age" : 25 }
{ "name" : "Liuneng", "age" : 26 }
{ "name" : "Liuneng222" }
{  }

6. 大小写

mongodb大小写敏感,开启mongdb服务后,在数据库test2里创建三个collection,虽然名子“一样”,但由于大小写不一样,所以是不同的集合
MongoDB Enterprise > use test2
switched to db test2
MongoDB Enterprise > db.createCollection("Student")
{ "ok" : 1 }
MongoDB Enterprise >   db.createCollection("student")
{ "ok" : 1 }
MongoDB Enterprise > db.createCollection('STUDENT')
{ "ok" : 1 }
MongoDB Enterprise > show collections
STUDENT
Student
student
MongoDB Enterprise > db.createCollection("student")
{
        "ok" : 0,
        "errmsg" : "a collection 'test2.student' already exists",
        "code" : 48,
        "codeName" : "NamespaceExists"
}