一、find
1、简单查询
#查寻所有文档
>db.foo.find()#查询包含name=hgq的文档
>db.foo.find({"name":"hgq"})
#查询包含name=hgq,age=12的文档,默认为全部条件都匹配
>db.foo.find({"name":"hgq","age":12})
2、返回指定的键
> db.foo.find({"name":"hgq"},{"name":1,"_id":0})
{ "name" : "hgq" }
前面的{}中为匹配条件,后面{}中的为要显示的键值,1为显示,由于_id是默认会显示的,可以通过0来不显示
二、查询条件
1、查询条件
常见的比较符,“ l t ”(小于),“ lt”(小于),“ lt”(小于),“lte”(小于等于),“ g t ”(大于),“ gt”(大于),“ gt”(大于),“gte”(大于等于)
比如查询一个数值范围,大于等于5,小于等于12的
>db.foo.find({"age":{"$gte":5,"$lte":12}})
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
也可以使用“$ ne”(不等于)来进行查询
>db.foo.find({"name":{"$ne":"hgq"}})
2、OR查询
mongodb中有两种方式进行OR查询,“ i n ”可以用来查询一个键的多个值,“ in”可以用来查询一个键的多个值,“ in”可以用来查询一个键的多个值,“or”更加通用,可以在多个键中查询给定的值
> db.foo.find({"hgq":{"$in":["11","22"]}})
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
{ "_id" : ObjectId("666febfaa48bbc31011e3059"), "hgq" : [ "11", "22", "44" ] }
{ "_id" : ObjectId("666fec7fa48bbc31011e305b"), "hgq" : [ "11", "22", "44", "55" ] }
类似的还有“$nin”,代表不包含
> db.foo.find({"hgq":{"$nin":["11","22"]}})
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
{ "_id" : ObjectId("666fde755d80c26396aa6105"), "hgq" : "tzy", "age" : 45 }
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("666fde8a5d80c26396aa6107"), "hgq" : "tutu", "age" : 2 }
"$or"查询多个条件,只要满足其一即可进行匹配
> db.foo.find({"$or":[{"hgq":"123"},{"age":123}]})
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
也可以将 o r 与 or与 or与in组合起来进行使用,当age=123或者hgq里面包含11、22的
> db.foo.find({"$or":[{"hgq":{"$in":["11","22"]}},{"age":123}]})
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
{ "_id" : ObjectId("666febfaa48bbc31011e3059"), "hgq" : [ "11", "22", "44" ] }
{ "_id" : ObjectId("666fec7fa48bbc31011e305b"), "hgq" : [ "11", "22", "44", "55" ] }
三、特定类型的查询
1、null
null可以将值等于空的匹配出来,也可以将没有这个键的文档匹配出来
> db.foo.find()
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
{ "_id" : ObjectId("666fde755d80c26396aa6105"), "hgq" : "tzy", "age" : 45 }
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("666fde8a5d80c26396aa6107"), "hgq" : "tutu", "age" : 2 }
{ "_id" : ObjectId("666fdf695d80c26396aa6108"), "hgq" : "111", "type" : null } ] }
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
> db.foo.find({"hgq":null})
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
{ "_id" : ObjectId("6671645c02eed0c7ea6236da"), "age" : 11, "hgq" : null }
如果只想匹配存在这个键值hgq,并且值为null的,
> db.foo.find({"hgq":{"$in":[null],"$exists":true}})
{ "_id" : ObjectId("6671645c02eed0c7ea6236da"), "age" : 11, "hgq" : null }
2、正则表达式
可以使用正则表达式进行忽略大小写
> db.foo.find({"name":/HGQ/i})
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
{ "_id" : ObjectId("66716574ea376546f316a6ac"), "name" : "Hgq" }
{ "_id" : ObjectId("66716578ea376546f316a6ad"), "name" : "HGQ" }
{ "_id" : ObjectId("6671657bea376546f316a6ae"), "name" : "HGQ1" }
{ "_id" : ObjectId("6671657dea376546f316a6af"), "name" : "HGQ12" }
3、查询数组
显示包含11、22的文档$all
> db.foo.find({"hgq":{$all:["11","22"]}})
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
{ "_id" : ObjectId("666febfaa48bbc31011e3059"), "hgq" : [ "11", "22", "44" ] }
{ "_id" : ObjectId("666fec7fa48bbc31011e305b"), "hgq" : [ "11", "22", "44", "55" ] }
显示下标为2等于33的数组
> db.foo.find({"hgq.2":"33"})
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
查询特定长度的集合$size
> db.foo.find({"hgq":{"$size":3}})
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
{ "_id" : ObjectId("666febfaa48bbc31011e3059"), "hgq" : [ "11", "22", "44" ] }
4、查询内嵌文档
可以使用点表示法查询内嵌文档的键
> db.foo.find({"name.ff":"ff"})
{ "_id" : ObjectId("66723fa90f2684cca53556a6"), "name" : { "ff" : "ff", "ll" : "ll" }, "age" : 123 }
> db.foo.find({"name.ff":"ff","name.ll":"ll"})
{ "_id" : ObjectId("66723fa90f2684cca53556a6"), "name" : { "ff" : "ff", "ll" : "ll" }, "age" : 123 }
更加复杂的查询,“$elemMatch”对内嵌文档进行分组,只有在对一个内嵌文档中的多个键操作时才会用到
> db.foo.find({"hgq":{"$elemMatch":{"tt":4,"tuut":{"$gte":2}}}})
{ "_id" : ObjectId("6670056486d64f913b307096"), "hgq" : [ { "tt" : 3, "tutu" : 4 }, { "tt" : 4, "tuut" : 3 } ] }
> db.foo.find({"hgq":{"$elemMatch":{"tt":4}}})
{ "_id" : ObjectId("6670056486d64f913b307096"), "hgq" : [ { "tt" : 3, "tutu" : 4 }, { "tt" : 4, "tuut" : 3 } ] }
四、游标
1、limit、skip与sort
limit限制返回的数量
> db.foo.find().limit(3)
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }
{ "_id" : ObjectId("666a723b97a427d1d07460b8"), "age" : 123 }
{ "_id" : ObjectId("666fde755d80c26396aa6105"), "hgq" : "tzy", "age" : 45 }
skip会跳过定义的前面的匹配数量,下面为不显示前三个的文档
> db.foo.find().skip(3)
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("666fde8a5d80c26396aa6107"), "hgq" : "tutu", "age" : 2 }
{ "_id" : ObjectId("666fdf695d80c26396aa6108"), "hgq" : "111", "type" : null }
{ "_id" : ObjectId("666feab6a48bbc31011e3058"), "hgq" : [ "11", "22", "33" ] }
{ "_id" : ObjectId("666febfaa48bbc31011e3059"), "hgq" : [ "11", "22", "44" ] }
sort对查询结果进行排序 1为升序,-1为降序
> db.foo.find().sort({"age":1})
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }
{ "_id" : ObjectId("666fde8a5d80c26396aa6107"), "hgq" : "tutu", "age" : 2 }
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
{ "_id" : ObjectId("666fde755d80c26396aa6105"), "hgq" : "tzy", "age" : 45 }
> db.foo.find().sort({"age":-1})
{ "_id" : ObjectId("666fde755d80c26396aa6105"), "hgq" : "tzy", "age" : 45 }
{ "_id" : ObjectId("66714e5a943d8ff6b7fefa95"), "name" : "hgq", "age" : 12 }
{ "_id" : ObjectId("666fde7e5d80c26396aa6106"), "hgq" : "tt", "age" : 5 }
{ "_id" : ObjectId("666fde8a5d80c26396aa6107"), "hgq" : "tutu", "age" : 2 }
{ "_id" : ObjectId("666a6e7e97a427d1d07460b5"), "hgq" : "123" }