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)

HBase文档操作--练习篇的更多相关文章

  1. MongoDB入门---文档操作之增删改

    之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文.     首先来看一下它的数据结构: ...

  2. MongoDB系列---集合与文档操作03

    MongoDB-——Collection 学习大纲: 1.集合操作 2.文档操作 知识回顾: 上一篇我们讲述了如何对MongoDB的权限和用户进行日常的基本操作,来达到我们对数据库的基本安全保障. 一 ...

  3. 一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)

    在目前的软件项目中,都会较多的使用到对文档的操作,用于记录和统计相关业务信息.由于系统自身提供了对文档的相关操作,所以在一定程度上极大的简化了软件使用者的工作量. 在.NET项目中如果用户提出了相关文 ...

  4. jQuery 核心 - noConflict() 方法,jQuery 文档操作 - detach() 方法

    原文地址:http://www.w3school.com.cn/jquery/manipulation_detach.asp   实例 使用 noConflict() 方法为 jQuery 变量规定新 ...

  5. XML文档操作之JAXP下实现

    JAXP是java API for xml PRocessing的缩写. 其API可以在javax.xml.parsers 这个包中找到.这个包向用户提供了两个最重要的工厂类,SAXParserFac ...

  6. jQuery文档操作

    jQuery文档操作 1.jq文档结构 var $sup = $('.sup'); $sup.children(); // sup所有的子级们 $sup.parent(); // sup的父级(一个, ...

  7. 06-jQuery的文档操作

    之前js中咱们学习了js的DOM操作,也就是所谓的增删改查DOM操作.通过js的DOM的操作,大家也能发现,大量的繁琐代码实现我们想要的效果.那么jQuery的文档操作的API提供了便利的方法供我们操 ...

  8. 06-jQuery的文档操作(重点)

    之前js中咱们学习了js的DOM操作,也就是所谓的增删改查DOM操作.通过js的DOM的操作,大家也能发现,大量的繁琐代码实现我们想要的效果.那么jQuery的文档操作的API提供了便利的方法供我们操 ...

  9. Jquery----对文档操作

    jquery对css操作: 1.CSS .css() - .css("color") -> 获取color css值 - .css("color", &q ...

随机推荐

  1. 静态代理、动态代理和cglib代理

    转:https://www.cnblogs.com/cenyu/p/6289209.html 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处 ...

  2. hihocoder #1068 : RMQ-ST算法 ( RMQ算法 O(nlogn)处理 O(1)查询 *【模板】 1)初始化d数组直接读入+计算k值用数学函数log2()==*节约时间 )

    #1068 : RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备 ...

  3. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

  4. expect的安装与使用

    expect的安装与使用 是什么 expect 是用来进行自动化控制和测试的工具.主要是和交互式软件telnet ftp ssh 等进行自动化的交互. 如何安装 1.检测是否安装 ls /usr/bi ...

  5. java android 将 List中元素互换位置

    很多时候我要对List中的元素调换位置,这时候可以用如下代码,意思是将data中的index1与index2元素互换位置 //data 为List Collections.swap(data,inde ...

  6. html5--6-28 css盒模型4

    html5--6-28 css盒模型4 实例 学习要点 了解盒模型 元素内容.内边距.边框 和 外边距 了解盒模型的概念: CSS 盒模型规定了处理元素内容.内边距.边框 和 外边距 的方式. 最内部 ...

  7. dba操作之archivelog清理

    下面的命令用于校验归档日志的有效性,列出无效的归档日志,以及以何种方式清除归档日志,列出几种常用的: crosscheck archivelog all;                        ...

  8. asp.net core zipkin

    微服务监控zipkin+asp.net core 0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 监控目录:微服务监控zipkin.skywalking以及日志ELK监控系列 一 ...

  9. UILabel 常见问题总结

    写在前面:笔者在iOS软件开发中发现UILabel控件有些问题反复出现,所以在这里做点总结,方便自己查阅,也能给大家提供相关问题的解决方案. 一:当label里的内容显示满了的时候,能够自动将字体变小 ...

  10. BZOJ_1406_[AHOI2007]密码箱_枚举+数学

    BZOJ_1406_[AHOI2007]密码箱_枚举+数学 Description 在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子 ...