标签:mail 利用 china 数学 地方 country 书籍 韩国 集合
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"})
db.persons.find({},{name:1,age:1,country:1,_id:0})
查询出年龄在25到27岁之间的学生
db.persons.find({age: {$gte:25,$lte:27},{_id:0,age:1})
查询出所有不是韩国籍的学生的数学成绩
db.persons.find({country:{$ne:"Korea"}},{_id:0,m:1})
查询国籍是中国或美国的学生信息
db.persons.find({country:{$in:["USA","China"]}})
查询国籍不是中国或美国的学生信息
db.persons.find({country:{$nin:["USA","China"]}})
查询语文成绩大于85或者英语大于90的学生信息
db.persons.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]},{_id:0,c:1,e:1})
把中国国籍的学生上增加新的键sex
db.person.update({country:"China"},{$set:{sex:"m"}})
查询出sex 等于 null的学生
db.persons.find({sex:{$in:[null]}},{country:1})
查询出名字中存在”li”的学生的信息
db.persons.find({name:/li/i},{_id:0,name:1})
查询出名字中不存在”li”的学生的信息
db.persons.find({name:{$not:/li/i}},{_id:0,name:1})
$not和$nin的区别是$not可以用在任何地方儿$nin是用到集合上的
查询喜欢看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})
增加字段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}})
查询出Jim书架中第2~4本书
db.persons.find({name:"jim"},{books:{"$slice":[1,3]}})
查询出最后一本书
db.persons.find({name:"jim"},{books:{"$slice":-1},_id:0,name:1})
为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"}}})
查询出persons文档中前5条数据
db.persons.find({},{_id:0,name:1}).limit(5)
查询出persons文档中5~10条的数据
db.persons.find({},{_id:0,name:1}).limit(5).skip(5)
db.persons.find({},{_id:0,name:1,age:1}).sort({age:1})
标签:mail 利用 china 数学 地方 country 书籍 韩国 集合
原文地址:http://www.cnblogs.com/jinxt/p/6829866.html