1.查询单条记录
2.查询时指定查询条件
默认{}查询全部
指定返回列
指定返回title和results属性
默认会返回_id,也可以设置不返回
3.条件查询
条件操作符
"$lt"===============>"<"
"$lte"==============>"<="
"$gt"===============>">"
"$gte"==============>">="
"$ne"==============>"!="
查询出blankCount大于等于1,小于等于2的一条记录
4.$or或
查询出blankCount为2或者type为3的一条记录
5.$not
查询出type不大于3的一条记录
6.查询空null的记录
设置字段为null后,字段为null或者不包含该字段的记录也会匹配。
如果不查询不存在的字段,则使用$exists:true
7.查询时使用正则表达式
查询title以While开头的一条记录
8.数组查询
查询数组questionIds中包含6188e9fc的记录
如果查询多个元素在数组中用$all,其中不分顺序。
精确匹配:
9.$slice操作符
取出数组中的前3条记录
取出数组中的后3条记录
10.内嵌文档查询
查询文档有两种方式,一种是完全匹查询,另一种是针对键值对查询。内嵌文档的完全匹配查询和数组的完全匹配查询一样,内嵌文档内键值对的数量,顺序都必须一致才会匹配。
完全匹配:
键值对匹配(常用):
数组中单条文档进行匹配时,使用$elemMatch
11.$where查询
查询出blankCount和type相等的一条记录
> db.choice.findOne()
2.查询时指定查询条件
> db.choice.find({"_id":"005a38d5"})
默认{}查询全部
指定返回列
> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1})
{ "_id" : "005a38d5", "title" : "While some maintain that the recent proliferati
on of uncredited web sources will have a(n) (i)_____ effect on scholarship, othe
rs argue that the effects will be far more (ii)_____, claiming that academics ar
e sensible enough not to (iii)_____ unattributed sources.", "results" : [ [ "1
" ], [ "1" ], [ "0" ] ] }
>
指定返回title和results属性
默认会返回_id,也可以设置不返回
> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1,"_id":0})
{ "title" : "While some maintain that the recent proliferation of uncredited web
sources will have a(n) (i)_____ effect on scholarship, others argue that the ef
fects will be far more (ii)_____, claiming that academics are sensible enough no
t to (iii)_____ unattributed sources.", "results" : [ [ "1" ], [ "1" ], [
"0" ] ] }
>
3.条件查询
条件操作符
"$lt"===============>"<"
"$lte"==============>"<="
"$gt"===============>">"
"$gte"==============>">="
"$ne"==============>"!="
查询出blankCount大于等于1,小于等于2的一条记录
> db.choice.findOne({"blankCount":{$lte:2,$gte:1}},{"blankCount":1})
{ "_id" : "006526ff", "blankCount" : 2 }
>
4.$or或
> db.choice.findOne({$or:[{"blankCount":2},{"type":3}]})
查询出blankCount为2或者type为3的一条记录
5.$not
> db.choice.findOne({"type":{"$not":{$gt:3}}})
查询出type不大于3的一条记录
6.查询空null的记录
> db.questionSet.findOne({source:null})
设置字段为null后,字段为null或者不包含该字段的记录也会匹配。
如果不查询不存在的字段,则使用$exists:true
> db.questionSet.findOne({source:null,$exists:true})
7.查询时使用正则表达式
> db.choice.findOne({title:/^While/})
查询title以While开头的一条记录
8.数组查询
> db.questionSet.findOne({"questionIds":'6188e9fc'},{"questionIds":1})
{
"_id" : "030eeeba",
"questionIds" : [
"6188e9fc",
"a380e38c",
"addff709",
"b6bc4eff",
"5095b99f",
"c8352e48",
"ecca3626",
"c31125f7"
]
}
查询数组questionIds中包含6188e9fc的记录
如果查询多个元素在数组中用$all,其中不分顺序。
> db.questionSet.findOne({"questionIds":{$all:['6188e9fc','a380e38c']}},{"questi
onIds":1})
{
"_id" : "030eeeba",
"questionIds" : [
"6188e9fc",
"a380e38c",
"addff709",
"b6bc4eff",
"5095b99f",
"c8352e48",
"ecca3626",
"c31125f7"
]
}
精确匹配:
> db.questionSet.findOne({"questionIds":['6188e9fc','a380e38c']},{"questionIds":
1})
null
9.$slice操作符
取出数组中的前3条记录
> db.questionSet.findOne({},{"questionIds":{$slice:3}})
{
"_id" : "030eeeba",
"catQuestionSet" : 2,
"orderNo" : 2,
"source" : 1,
"type" : 2,
"level" : 3,
"questionCount" : 10,
"questionIds" : [
"6188e9fc",
"a380e38c",
"addff709"
]
}
取出数组中的后3条记录
> db.questionSet.findOne({},{"questionIds":{$slice:-3}})
{
"_id" : "030eeeba",
"catQuestionSet" : 2,
"orderNo" : 2,
"source" : 1,
"type" : 2,
"level" : 3,
"questionCount" : 10,
"questionIds" : [
"c8352e48",
"ecca3626",
"c31125f7"
]
}
10.内嵌文档查询
查询文档有两种方式,一种是完全匹查询,另一种是针对键值对查询。内嵌文档的完全匹配查询和数组的完全匹配查询一样,内嵌文档内键值对的数量,顺序都必须一致才会匹配。
完全匹配:
> db.choice.findOne({"explain":{"ccurlList":"3DC334A16B187EBF9C33DC5901307461","
textExplain":"Answers"}})
键值对匹配(常用):
> db.choice.findOne({"explain.ccurlList":"3DC334A16B187EBF9C33DC5901307461","exp
lain.textExplain":"Answers"})
数组中单条文档进行匹配时,使用$elemMatch
> db.choice.findOne({"explain":{$elemMatch:{"ccurlList":"3DC334A16B187EBF9C33DC5
901307461","textExplain":"Answers"}}})
11.$where查询
查询出blankCount和type相等的一条记录
> db.choice.findOne({$where:"this.blankCount==this.type"})
{
"_id" : "005a38d5",
"blankCount" : 3,
"explain" : {
"ccurlList" : [ ]
},
"type" : 3,
"questionSetId" : "affccc14"
}