MongoDB查询操作

1 数据准备

var persons = [{
 name:"jim",
 age:25,
 email:"75431457@qq.com",
 c:89,m:96,e:87,
 country:"USA",
 books:["JS","C++","EXTJS","MONGODB"]
},
{
 name:"tom",
 age:25,
 email:"214557457@qq.com",
 c:75,m:66,e:97,
 country:"USA",
 books:["PHP","JAVA","EXTJS","C++"]
},
{
 name:"lili",
 age:26,
 email:"344521457@qq.com",
 c:75,m:63,e:97,
 country:"USA",
 books:["JS","JAVA","C#","MONGODB"]
},
{
 name:"zhangsan",
 age:27,
 email:"2145567457@qq.com",
 c:89,m:86,e:67,
 country:"China",
 books:["JS","JAVA","EXTJS","MONGODB"]
},
{
 name:"lisi",
 age:26,
 email:"274521457@qq.com",
 c:53,m:96,e:83,
 country:"China",
 books:["JS","C#","PHP","MONGODB"]
},
{
 name:"wangwu",
 age:27,
 email:"65621457@qq.com",
 c:45,m:65,e:99,
 country:"China",
 books:["JS","JAVA","C++","MONGODB"]
},
{
 name:"zhaoliu",
 age:27,
 email:"214521457@qq.com",
 c:99,m:96,e:97,
 country:"China",
 books:["JS","JAVA","EXTJS","PHP"]
},
{
 name:"piaoyingjun",
 age:26,
 email:"piaoyingjun@uspcat.com",
 c:39,m:54,e:53,
 country:"Korea",
 books:["JS","C#","EXTJS","MONGODB"]
},
{
 name:"lizhenxian",
 age:27,
 email:"lizhenxian@uspcat.com",
 c:35,m:56,e:47,
 country:"Korea",
 books:["JS","JAVA","EXTJS","MONGODB"]
},
{
 name:"lixiaoli",
 age:21,
 email:"lixiaoli@uspcat.com",
 c:36,m:86,e:32,
 country:"Korea",
 books:["JS","JAVA","PHP","MONGODB"]
},
{
 name:"zhangsuying",
 age:22,
 email:"zhangsuying@uspcat.com",
 c:45,m:63,e:77,
 country:"Korea",
 books:["JS","JAVA","C#","MONGODB"]
}]
for(var i = 0;i<persons.length;i++){
 db.persons.insert(persons[i])
}
var persons = db.persons.find({name:"jim"})

2 查询

2.1 查询出所有数据的指定键(name ,age ,country)

  db.persons.find({},{name:1,age:1,country:1,_id:0})

2.2 查询条件

查询出年龄在25到27岁之间的学生

   db.persons.find({age: {$gte:25,$lte:27},{_id:0,age:1})

查询出所有不是韩国籍的学生的数学成绩

   db.persons.find({country:{$ne:"Korea"}},{_id:0,m:1})

2.3 包含或不包含:$in或$nin

查询国籍是中国或美国的学生信息

   db.persons.find({country:{$in:["USA","China"]}})

查询国籍不是中国或美国的学生信息

   db.persons.find({country:{$nin:["USA","China"]}})

2.4 OR查询:$or

查询语文成绩大于85或者英语大于90的学生信息

   db.persons.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]},{_id:0,c:1,e:1})

2.5 Null

把中国国籍的学生上增加新的键sex

    db.person.update({country:"China"},{$set:{sex:"m"}})

查询出sex 等于 null的学生

    db.persons.find({sex:{$in:[null]}},{country:1})

2.6 正则查询

查询出名字中存在”li”的学生的信息

    db.persons.find({name:/li/i},{_id:0,name:1})

2.7 $not的使用,$not可以用到任何地方进行取反操作

查询出名字中不存在”li”的学生的信息

     db.persons.find({name:{$not:/li/i}},{_id:0,name:1})

$not和$nin的区别是$not可以用在任何地方儿$nin是用到集合上的

2.8 数组查询$all和index应用

查询喜欢看MONGOD和JS的学生

     db.persons.find({books:{$all:["MONGOBD","JS"]}},{books:1,_id:0})

查询第二本书是JAVA的学习信息

     db.persons.find({"books.1":"JAVA"})

查询指定长度数组$size它不能与比较查询符一起使用(这是弊端)

查询出喜欢的书籍数量是4本的学生

     db.persons.find({books:{$size:4}},{_id:0,books:1})

2.9 查询出喜欢的书籍数量大于3本的学生

增加字段size

     db.persons.update({},{$set:{size:4}},false, true)

改变书籍的更新方式,每次增加书籍的时候size增加1

     db.persons.update({查询器},{$push:{books:"ORACLE"},$inc:{size:1}})

利用$gt查询

     db.persons.find({size:{$gt:3}})

2.10 $slice操作符返回文档中指定数组的内部值

查询出Jim书架中第2~4本书

     db.persons.find({name:"jim"},{books:{"$slice":[1,3]}})

查询出最后一本书

     db.persons.find({name:"jim"},{books:{"$slice":-1},_id:0,name:1})

2.11 文档查询:$elemMatch

为jim添加学习简历文档

var jim = [{
 school :"K",
 score:"A"
},{
 school :"L",
 score:"B"
},{
 school :"J",
 score:"A+"
}]
     db.persons.update({name:"jim"},{$set:{school:jim}})

查询出在K上过学的学生

     db.persons.find({school:{$elemMatch:{school:"K",score:"A"}}})

2.12 Limit返回指定的数据条数

查询出persons文档中前5条数据

    db.persons.find({},{_id:0,name:1}).limit(5)

2.13 Skip返回指定数据的跨度

查询出persons文档中5~10条的数据

    db.persons.find({},{_id:0,name:1}).limit(5).skip(5)

2.14 .Sort返回按照年龄排序的数据[1,-1]

    db.persons.find({},{_id:0,name:1,age:1}).sort({age:1})

posted on 2017-05-09 11:42  上山捉蛇咬仙人  阅读(177)  评论(0编辑  收藏  举报

导航