1.查询学生的所有信息
数据准备
var persons = [{
name:"jim",
age:25,
email:"75431457@qq.com",
c:89,m:96,e:87,
country:"USA",
books:["JS","C++","EXTJS","MONGODB"],
address:{city:"beijing",street:"861 Park Street"}
},
{
name:"tom",
age:null,
email:"214557457@qq.com",
c:75,m:66,e:97,
country:"USA",
books:["PHP","JAVA","EXTJS","C++"],
address:{city:"beijing",province:"hebei"}
},
{
name:"lili",
age:null,
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:"Limiao",
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"]
}]
先执行
db.studend.find()
若存在测删除student
db.student.drop()
再执行插入
db.student.insert(persons)
>db.student.insert(persons)
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 12,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
然后查询所有student记录
>db.student.find()
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c77"), "name" : "lisi", "age" : 26, "email" : "274521457@qq.com", "c" : 53, "m" : 96, "e" : 83, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7b"), "name" : "lizhenxian", "age" : 27, "email" : "lizhenxian@uspcat.com", "c" : 35, "m" : 56, "e" : 47, "country" : "Korea", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7c"), "name" : "lixiaoli", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7d"), "name" : "Limiao", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7e"), "name" : "zhangsuying", "age" : 22, "email" : "zhangsuying@uspcat.com", "c" : 45, "m" : 63, "e" : 77, "country" : "Korea", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
也可以使用这个命令
>db.student.find({})
2.查询所有学生的姓名
> db.student.find({},{_id:0,name:1})
{ "name" : "jim" }
{ "name" : "tom" }
{ "name" : "lili" }
{ "name" : "zhangsan" }
{ "name" : "lisi" }
{ "name" : "wangwu" }
{ "name" : "zhaoliu" }
{ "name" : "piaoyingjun" }
{ "name" : "lizhenxian" }
{ "name" : "lixiaoli" }
{ "name" : "Limiao" }
{ "name" : "zhangsuying" }
3.查询出年龄在25到27岁之间的学生
> db.student.find({age:{$gte:25,$lte:27}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c77"), "name" : "lisi", "age" : 26, "email" : "274521457@qq.com", "c" : 53, "m" : 96, "e" : 83, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7b"), "name" : "lizhenxian", "age" : 27, "email" : "lizhenxian@uspcat.com", "c" : 35, "m" : 56, "e" : 47, "country" : "Korea", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
下面这种方式更直观
db.student.find({age:{$gte:25,$lte:27}},{_id:0,name:1,age:1})
{ "name" : "jim", "age" : 25 }
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "lisi", "age" : 26 }
{ "name" : "wangwu", "age" : 27 }
{ "name" : "zhaoliu", "age" : 27 }
{ "name" : "piaoyingjun", "age" : 26 }
{ "name" : "lizhenxian", "age" : 27 }
4.查询出所有不是韩国籍的学生
db.student.find({country:{$ne:"Korea"}},{_id:0,name:1,country:1})
{ "name" : "jim", "country" : "USA" }
{ "name" : "tom", "country" : "USA" }
{ "name" : "lili", "country" : "USA" }
{ "name" : "zhangsan", "country" : "China" }
{ "name" : "lisi", "country" : "China" }
{ "name" : "wangwu", "country" : "China" }
{ "name" : "zhaoliu", "country" : "China" }
5.查询国籍是中国或美国的学生信息
> db.student.find({country:{$in:["USA","China"]}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c77"), "name" : "lisi", "age" : 26, "email" : "274521457@qq.com", "c" : 53, "m" : 96, "e" : 83, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
6.查询国籍不是中国或美国的学生信息
> db.student.find({country:{$nin:["USA","China"]}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7b"), "name" : "lizhenxian", "age" : 27, "email" : "lizhenxian@uspcat.com", "c" : 35, "m" : 56, "e" : 47, "country" : "Korea", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7c"), "name" : "lixiaoli", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7d"), "name" : "Limiao", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7e"), "name" : "zhangsuying", "age" : 22, "email" : "zhangsuying@uspcat.com", "c" : 45, "m" : 63, "e" : 77, "country" : "Korea", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
7.查询语文成绩大于85并且英语大于90的学生信息
db.student.find({$and:[{c:{$gte:85}},{e:{$gte:90}}]})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
8.查询语文成绩大于85或者英语大于90的学生信息
> db.student.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
>
9.查询出名字中不存在”li”的学生的信息
> db.student.find({name:{$not:/li/i}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7e"), "name" : "zhangsuying", "age" : 22, "email" : "zhangsuying@uspcat.com", "c" : 45, "m" : 63, "e" : 77, "country" : "Korea", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
10.查询地址存在的学生信息
> db.student.find({address:{$exists:true}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
11.查询年龄为空值的学生信息
> db.student.find({address:{$exists:true}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
或者使用这个
> db.student.find({age:null})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
> db.student.find({age:{$type:10}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
12.查询address为{city:
> db.student.find({address:{city:"beijing",street:"861 Park Street"}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
> db.student.find({"address.city":"beijing"})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "PHP", "JAVA", "EXTJS", "C++" ], "address" : { "city" : "beijing", "province" : "hebei" } }
>
数组查询
> db.student.find({books:["JS","C#","EXTJS","MONGODB"]})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
> db.student.find({books:"JS"})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c77"), "name" : "lisi", "age" : 26, "email" : "274521457@qq.com", "c" : 53, "m" : 96, "e" : 83, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7b"), "name" : "lizhenxian", "age" : 27, "email" : "lizhenxian@uspcat.com", "c" : 35, "m" : 56, "e" : 47, "country" : "Korea", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7c"), "name" : "lixiaoli", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7d"), "name" : "Limiao", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JS", "JAVA", "PHP", "MONGODB" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7e"), "name" : "zhangsuying", "age" : 22, "email" : "zhangsuying@uspcat.com", "c" : 45, "m" : 63, "e" : 77, "country" : "Korea", "books" : [ "JS", "JAVA", "C#", "MONGODB" ] }
>
> db.student.find({},{books:{$slice:[1,2]}})
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c73"), "name" : "jim", "age" : 25, "email" : "75431457@qq.com", "c" : 89, "m" : 96, "e" : 87, "country" : "USA", "books" : [ "C++", "EXTJS" ], "address" : { "city" : "beijing", "street" : "861 Park Street" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c74"), "name" : "tom", "age" : null, "email" : "214557457@qq.com", "c" : 75, "m" : 66, "e" : 97, "country" : "USA", "books" : [ "JAVA", "EXTJS" ], "address" : { "city" : "beijing", "province" : "hebei" } }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c75"), "name" : "lili", "age" : null, "email" : "344521457@qq.com", "c" : 75, "m" : 63, "e" : 97, "country" : "USA", "books" : [ "JAVA", "C#" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c76"), "name" : "zhangsan", "age" : 27, "email" : "2145567457@qq.com", "c" : 89, "m" : 86, "e" : 67, "country" : "China", "books" : [ "JAVA", "EXTJS" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c77"), "name" : "lisi", "age" : 26, "email" : "274521457@qq.com", "c" : 53, "m" : 96, "e" : 83, "country" : "China", "books" : [ "C#", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c78"), "name" : "wangwu", "age" : 27, "email" : "65621457@qq.com", "c" : 45, "m" : 65, "e" : 99, "country" : "China", "books" : [ "JAVA", "C++" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c79"), "name" : "zhaoliu", "age" : 27, "email" : "214521457@qq.com", "c" : 99, "m" : 96, "e" : 97, "country" : "China", "books" : [ "JAVA", "EXTJS" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7a"), "name" : "piaoyingjun", "age" : 26, "email" : "piaoyingjun@uspcat.com", "c" : 39, "m" : 54, "e" : 53, "country" : "Korea", "books" : [ "C#", "EXTJS" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7b"), "name" : "lizhenxian", "age" : 27, "email" : "lizhenxian@uspcat.com", "c" : 35, "m" : 56, "e" : 47, "country" : "Korea", "books" : [ "JAVA", "EXTJS" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7c"), "name" : "lixiaoli", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JAVA", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7d"), "name" : "Limiao", "age" : 21, "email" : "lixiaoli@uspcat.com", "c" : 36, "m" : 86, "e" : 32, "country" : "Korea", "books" : [ "JAVA", "PHP" ] }
{ "_id" : ObjectId("57554f9cf7b6c25c4d573c7e"), "name" : "zhangsuying", "age" : 22, "email" : "zhangsuying@uspcat.com", "c" : 45, "m" : 63, "e" : 77, "country" : "Korea", "books" : [ "JAVA", "C#" ] }
>
游标查询
var cursor = db.student.find({})
>
> while(cursor.hasNext()){
... printjson(cursor.next())
... }
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c73"),
"name" : "jim",
"age" : 25,
"email" : "75431457@qq.com",
"c" : 89,
"m" : 96,
"e" : 87,
"country" : "USA",
"books" : [
"JS",
"C++",
"EXTJS",
"MONGODB"
],
"address" : {
"city" : "beijing",
"street" : "861 Park Street"
}
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c74"),
"name" : "tom",
"age" : null,
"email" : "214557457@qq.com",
"c" : 75,
"m" : 66,
"e" : 97,
"country" : "USA",
"books" : [
"PHP",
"JAVA",
"EXTJS",
"C++"
],
"address" : {
"city" : "beijing",
"province" : "hebei"
}
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c75"),
"name" : "lili",
"age" : null,
"email" : "344521457@qq.com",
"c" : 75,
"m" : 63,
"e" : 97,
"country" : "USA",
"books" : [
"JS",
"JAVA",
"C#",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c76"),
"name" : "zhangsan",
"age" : 27,
"email" : "2145567457@qq.com",
"c" : 89,
"m" : 86,
"e" : 67,
"country" : "China",
"books" : [
"JS",
"JAVA",
"EXTJS",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c77"),
"name" : "lisi",
"age" : 26,
"email" : "274521457@qq.com",
"c" : 53,
"m" : 96,
"e" : 83,
"country" : "China",
"books" : [
"JS",
"C#",
"PHP",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c78"),
"name" : "wangwu",
"age" : 27,
"email" : "65621457@qq.com",
"c" : 45,
"m" : 65,
"e" : 99,
"country" : "China",
"books" : [
"JS",
"JAVA",
"C++",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c79"),
"name" : "zhaoliu",
"age" : 27,
"email" : "214521457@qq.com",
"c" : 99,
"m" : 96,
"e" : 97,
"country" : "China",
"books" : [
"JS",
"JAVA",
"EXTJS",
"PHP"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c7a"),
"name" : "piaoyingjun",
"age" : 26,
"email" : "piaoyingjun@uspcat.com",
"c" : 39,
"m" : 54,
"e" : 53,
"country" : "Korea",
"books" : [
"JS",
"C#",
"EXTJS",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c7b"),
"name" : "lizhenxian",
"age" : 27,
"email" : "lizhenxian@uspcat.com",
"c" : 35,
"m" : 56,
"e" : 47,
"country" : "Korea",
"books" : [
"JS",
"JAVA",
"EXTJS",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c7c"),
"name" : "lixiaoli",
"age" : 21,
"email" : "lixiaoli@uspcat.com",
"c" : 36,
"m" : 86,
"e" : 32,
"country" : "Korea",
"books" : [
"JS",
"JAVA",
"PHP",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c7d"),
"name" : "Limiao",
"age" : 21,
"email" : "lixiaoli@uspcat.com",
"c" : 36,
"m" : 86,
"e" : 32,
"country" : "Korea",
"books" : [
"JS",
"JAVA",
"PHP",
"MONGODB"
]
}
{
"_id" : ObjectId("57554f9cf7b6c25c4d573c7e"),
"name" : "zhangsuying",
"age" : 22,
"email" : "zhangsuying@uspcat.com",
"c" : 45,
"m" : 63,
"e" : 77,
"country" : "Korea",
"books" : [
"JS",
"JAVA",
"C#",
"MONGODB"
]
}
>
使用正则表达式
> db.student.find({name:{$regex:/l/}},{_id:0,name:1})
{ "name" : "lili" }
{ "name" : "lisi" }
{ "name" : "zhaoliu" }
{ "name" : "lizhenxian" }
{ "name" : "lixiaoli" }
> db.student.find({name:/^l/i},{_id:0,name:1})
{ "name" : "lili" }
{ "name" : "lisi" }
{ "name" : "lizhenxian" }
{ "name" : "lixiaoli" }
{ "name" : "Limiao" }
>
对查询结果排序
> db.student.find({},{_id:0,name:1,age:1}).sort({age:1})
{ "name" : "tom", "age" : null }
{ "name" : "lili", "age" : null }
{ "name" : "lixiaoli", "age" : 21 }
{ "name" : "Limiao", "age" : 21 }
{ "name" : "zhangsuying", "age" : 22 }
{ "name" : "jim", "age" : 25 }
{ "name" : "lisi", "age" : 26 }
{ "name" : "piaoyingjun", "age" : 26 }
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "wangwu", "age" : 27 }
{ "name" : "zhaoliu", "age" : 27 }
{ "name" : "lizhenxian", "age" : 27 }
> db.student.find({},{_id:0,name:1,age:1}).sort({name:-1,age:1})
{ "name" : "zhaoliu", "age" : 27 }
{ "name" : "zhangsuying", "age" : 22 }
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "wangwu", "age" : 27 }
{ "name" : "tom", "age" : null }
{ "name" : "piaoyingjun", "age" : 26 }
{ "name" : "lizhenxian", "age" : 27 }
{ "name" : "lixiaoli", "age" : 21 }
{ "name" : "lisi", "age" : 26 }
{ "name" : "lili", "age" : null }
{ "name" : "jim", "age" : 25 }
{ "name" : "Limiao", "age" : 21 }
>
使用limit函数
> db.student.find({},{_id:0,name:1,age:1},2)
{ "name" : "jim", "age" : 25 }
{ "name" : "tom", "age" : null }
> db.student.find({},{_id:0,name:1,age:1},2,3)
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "lisi", "age" : 26 }
使用skip函数
> db.student.find({},{_id:0,name:1,age:1},2).skip(3)
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "lisi", "age" : 26 }
> db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(0)
{ "name" : "jim", "age" : 25 }
{ "name" : "tom", "age" : null }
{ "name" : "lili", "age" : null }
> db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(3)
{ "name" : "zhangsan", "age" : 27 }
{ "name" : "lisi", "age" : 26 }
{ "name" : "wangwu", "age" : 27 }
> db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(6)
{ "name" : "zhaoliu", "age" : 27 }
{ "name" : "piaoyingjun", "age" : 26 }
{ "name" : "lizhenxian", "age" : 27 }
疑难小结
本次实验没有什么太大的疑难问题,只要注意语法上的书写格式,还有符号细节,这些药注意,很多时候会因为少写了一个括号导致查询错误,不过根据错误提示一下就可以找出来,课后要多敲多练!
主要算法程序清单
db.student.drop(); --先删除
db.student.insert(persons); --再添加
2.查询所有的记录
db.student.find()
db.student.find({})
find()的语法介绍
3.投影操作,即是列的操作
db.student.find({},{_id:0})
db.student.find({},{_id:0,name:1})
要么写想要的字段,要么写不想要的字段,最好不要混合写
db.student.find({},{name:1,age:1})
db.student.find({},{_id:0,name:0,age:0})
db.student.find({},{age:0,name:1}) --混合写出错
db.student.find({},{_id:1,name:0}) --混合写出错
选择操作,即是行的操作
4.比较运算符
(1)大于等于$gte 小于等于$lte 不等于$ne
I.查询出年龄在25到27岁之间的学生
db.student.find({age:{$gte:25,$lte:27}})
db.student.find({age:{$gte:25,$lte:27}},{_id:0,name:1})
II.查询出所有不是韩国籍的学生
db.student.find({country:{$ne:"Korea"}})
db.student.find({country:{$ne:"Korea"}},{_id:0,name:1})
(2)包含或不包含
$in或$nin
I.查询国籍是中国或美国的学生信息
db.student.find({country:{$in:["USA","China"]}})
II.查询国籍不是中国或美国的学生信息
db.student.find({country:{$nin:["USA","China"]}})
5.逻辑运算符
$and
I.查询语文成绩大于85并且英语大于90的学生信息
db.student.find({$and:[{c:{$gte:85}},{e:{$gte:90}}]})
$or
II.查询语文成绩大于85或者英语大于90的学生信息
db.student.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]})
$not的使用
III.查询出名字中不存在”li”的学生的信息
db.student.find({name:{$not:/li/i}})
6.元素查询操作符
$exists
I.查询地址存在的学生信息
db.student.find({address:{$exists:true}})
II.查询年龄为空值的学生信息
db.student.find({age:null})
db.student.find({age:{$type:10}})
7.内嵌文档查询
db.student.find({address:{city:"beijing",street:"861 Park Street"}})
db.student.find({"address.city":"beijing"})
8.数组查询
db.student.find({books:["JS","C#","EXTJS","MONGODB"]})
db.student.find({books:"JS"})
db.student.find({"books.3":"PHP"})
db.student.find({books:{$all:["PHP","JS"]}})
db.student.find({},{books:{$slice:[1,2]}}) --跳过数组里面的第一个元素,返回两个元素
9.游标查询
var cursor = db.student.find({})
while(cursor.hasNext()){
printjson(cursor.next())
}
10.模糊查询
使用正则表达式
db.student.find({name:{$regex:/l/}},{_id:0,name:1})
db.student.find({name:{$regex:/l/i}},{_id:0,name:1})
db.student.find({name:{$regex:/^l/i}},{_id:0,name:1})
db.student.find({name:/^l/i},{_id:0,name:1})
11.查询结果排序
db.student.find({},{_id:0,name:1,age:1}).sort({age:1})
db.student.find({},{_id:0,name:1,age:1}).sort({age:-1})
db.student.find({},{_id:0,name:1,age:1}).sort({name:-1,age:1})
12.limit 返回结果集中的前几个
a.使用第三个参数
db.student.find({},{_id:0,name:1,age:1},2)
b.使用limit()函数
13.skip 跳过一定数量
a.使用第四个参数
db.student.find({},{_id:0,name:1,age:1},2,3)
b.使用skip()函数
db.student.find({},{_id:0,name:1,age:1},2).skip(3)
*利用limit()和skip()实现分页技术
第一页 db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(0)
第二页 db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(3)
第三页 db.student.find({},{_id:0,name:1,age:1}).limit(3).skip(6)