MongoDB过关测试二
一、 其它 (共27题,135分)
1、创建数据库mydb,给指定的集合添加文档。
db.students.insertMany([
{"_id":“1001”,“name”:“程明”,“sex”:“男”,“age”:19,“major”:“计算机应用技术”,“credits”:52,score:92},
{"_id":“1002”,“name”:“刘萍”,“sex”:“女”,“age”:18,“major”:“移动应用开发”,“credits”:51,score:88},
{"_id":“1003”,“name”:“张静”,“sex”:“女”,“age”:20,“major”:“计算机应用技术”,“credits”:48,score:96},
{"_id":“1004”,“name”:“陈好”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:97},
{"_id":“1005”,“name”:“施行”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:54,score:60},
{"_id":“1006”,“name”:“刘文平”,“sex”:“女”,“age”:18,“major”:“软件技术”,“credits”:50,score:86},
{"_id":“1007”,“name”:“李德胜”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:55,score:95},
{"_id":“1008”,“name”:“苏锦州”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:50,score:70},
{"_id":“1009”,“name”:“张文杰”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:40},
{"_id":“1010”,“name”:“王林”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:47,score:87},
{"_id":“1011”,“name”:“程梦凡”,“sex”:“男”,“age”:19,“major”:“软件技术”,“credits”:46,score:66},
{"_id":“1012”,“name”:“陈玉婷”,“sex”:“女”,“age”:19,“major”:“计算机应用技术”,“credits”:45,score:82},
{"_id":“1013”,“name”:“张春华”,“sex”:“男”,“age”:19,“major”:“软件技术”,“credits”:45,score:64},
{"_id":“1014”,“name”:“苏秦”,“sex”:“女”,“age”:18,“major”:“计算机应用技术”,“credits”:41,score:77},
{"_id":“1015”,“name”:“李雨桐”,“sex”:“男”,“age”:21,“major”:“移动应用开发”,“credits”:49,score:81},
{"_id":“1016”,“name”:“唐开平”,“sex”:“女”,“age”:18,“major”:“软件技术”,“credits”:42,score:74},
{"_id":“1017”,“name”:“罗世杰”,“sex”:“男”,“age”:21,“major”:“移动应用开发”,“credits”:57,score:64},
{"_id":“1018”,“name”:“张可欣”,“sex”:“女”,“age”:20,“major”:“计算机应用技术”,“credits”:54,score:47},
{"_id":“1019”,“name”:“刘柔”,“sex”:“男”,“age”:22,“major”:“移动应用开发”,“credits”:61,score:81},
{"_id":“1020”,“name”:“王可可”,“sex”:“女”,“age”:17,“major”:“计算机应用技术”,“credits”:37,score:67},
{"_id":“1021”,“name”:“王林”,“sex”:“男”,“age”:21,“major”:“移动应用开发”,“credits”:61,score:95}
])
(5.0)
2、
db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"major",name:{
p
u
s
h
:
"
push:"
push:"name"},sex:{
p
u
s
h
:
"
push:"
push:"sex"},major:{
p
u
s
h
:
"
push:"
push:"major"},age:{
p
u
s
h
:
"
push:"
push:"age"}}},{KaTeX parse error: Expected 'EOF', got '}' at position 14: sort:{age:-1}}̲,{sort:{major:1}}])
3、
2.db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: match:{or:[{major:“计算机应用技术”},{major:“移动应用开发”}]}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"major",avgage:{
a
v
g
:
"
avg:"
avg:"age"}}}])
4、
db.students.aggregate([{KaTeX parse error: Expected 'EOF', got '}' at position 23: …major:"计算机应用技"}}̲,{group:{_id:"KaTeX parse error: Expected '}', got 'EOF' at end of input: sex",sumcre:{sum:“KaTeX parse error: Expected 'EOF', got '}' at position 9: credits"}̲,mincre:{min:“KaTeX parse error: Expected 'EOF', got '}' at position 9: credits"}̲,maxcre:{max:”$credits”}}}])
5、
db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"major",avgscore:{
a
v
g
:
"
avg:"
avg:"score"},firstscore:{
f
i
r
s
t
:
"
first:"
first:"score"},lastscore:{
l
a
s
t
:
"
last:"
last:"score"}}}])
6、
db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"major",account:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}},{sort:{account:1}}])
7、
db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: project:{name:"name",major:“
m
a
j
o
r
"
,
s
c
o
r
e
:
"
major",score:"
major",score:"score”}},{KaTeX parse error: Expected 'EOF', got '}' at position 16: sort:{score:-1}}̲,{skip:4},{$limit:2}])
8、
db.students.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: match:{age:{gt:18}}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:"major",account:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}},{match:{account:{$gt:4}}}])
9、
(5.0)
10、打开MongoDB,打开服务,连上客户端,切换到exam数据库,只需截切换到exam数据库这条命令及结果。
use exam
11、向students集合中添加如下文档:
{"_id":“1001”,“name”:“程明”,“sex”:“男”,“age”:19,“major”:“计算机应用技术”,“credits”:52,score:92}
{"_id":“1002”,“name”:“刘萍”,“sex”:“女”,“age”:18,“major”:“移动应用开发”,“credits”:51,score:88}
{"_id":“1003”,“name”:“张静”,“sex”:“女”,“age”:20,“major”:“计算机应用技术”,“credits”:48,score:96}
{"_id":“1004”,“name”:“陈好”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:97}
{"_id":“1005”,“name”:“施行”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:54,score:60}
{"_id":“1006”,“name”:“刘文平”,“sex”:“女”,“age”:18,“major”:“软件技术”,“credits”:50,score:86}
{"_id":“1007”,“name”:“李德胜”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:55,score:95}
{"_id":“1008”,“name”:“苏锦州”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:50,score:70}
{"_id":“1009”,“name”:“张文杰”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:40}
{"_id":“1010”,“name”:“王林”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:47,score:87}
(5.0)
12、请用查询语句显示计算机应用技术专业的学生人数。
db.students.find({major:“计算机应用技术”}).count()
(5.0)
13、请用查询语句显示年龄大于19岁的女生信息。
db.students.find({age:{$gt:19}})
(5.0)
14、请用查询语句显示计算机应用技术和软件技术专业成绩大于90分的学生姓名、性别、专业和成绩
db.students.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: …算机应用技术",score:{gt:90}},{major:“软件技术”,score:{$gt:90}}]},{name:1,sex:1,major:1,score:1})
(5.0)
15、用查询语句显示移动应用开发专业和计算机应用技术专业年龄为19-20岁女生的姓名、性别和成绩请 db.students.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: …:"移动应用开发",age:{gte:19},age:{lte:20}},{major:“软件技术”,age:{KaTeX parse error: Expected 'EOF', got '}' at position 7: gte:19}̲,age:{lte:20}}]},{name:1,sex:1,score:1})
(5.0)
16、请将陈好的专业(major)修改为移动应用开发。
db.students.update({name:“陈好”},{$set:{major:“移动应用开发”})
(5.0)
17、请给软件技术专业的所有女生添加一个字段course,值为MongoDB
db.students.update({major:“软件技术”,sex:“女”},{$set:{course:“MongoDB”},true,false})
(5.0)
18、请将李德胜的年龄减少2岁
db.students.update({name:“李德胜”},{$inc:{age:-2})
(5.0)
19、请删除计算机应用技术专业男生的成绩(score)字段
db.students.update({major:“计算机应用技术”,sex:“男”},{ $unset :{score:1}},false,true)
(5.0)
20、请删除所有成绩不及格的学生信息。
db.collection.remove({score:{$lt:60}})
(5.0)
21、查询所有男生的姓名,性别,年龄信息,并且按照年龄的降序显示
db.students.find({sex:“男”},{name:1,sex:1,age:1}).sort({age:-1})
(5.0)
22、请查询成绩最好的学生的姓名,年龄,专业和成绩信息
db.students.find({},{name:1,age:1,major:1,score:1}).sort({score:-1}).limit(1)
(5.0)
23、查找所有学生信息,按照专业升序,年龄降序排列。
db.students.find().sort({major:1,age:-1})
(5.0)
24、请删除学生集合。
db.students.drop()
(5.0)
25、向students集合中添加如下文档:
{"_id":“1001”,“name”:“程明”,“sex”:“男”,“age”:19,“major”:“计算机应用技术”,“credits”:52,score:92}
{"_id":“1002”,“name”:“刘萍”,“sex”:“女”,“age”:18,“major”:“移动应用开发”,“credits”:51,score:88}
{"_id":“1003”,“name”:“张静”,“sex”:“女”,“age”:20,“major”:“计算机应用技术”,“credits”:48,score:96}
{"_id":“1004”,“name”:“陈好”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:97}
{"_id":“1005”,“name”:“施行”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:54,score:60}
{"_id":“1006”,“name”:“刘文平”,“sex”:“女”,“age”:18,“major”:“软件技术”,“credits”:50,score:86}
{"_id":“1007”,“name”:“李德胜”,“sex”:“男”,“age”:20,“major”:“计算机应用技术”,“credits”:55,score:95}
{"_id":“1008”,“name”:“苏锦州”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:50,score:70}
{"_id":“1009”,“name”:“张文杰”,“sex”:“女”,“age”:20,“major”:“软件技术”,“credits”:53,score:40}
{"_id":“1010”,“name”:“王林”,“sex”:“男”,“age”:18,“major”:“移动应用开发”,“credits”:47,score:87}
(5.0)
26、请用查询语句显示计算机应用技术专业的学生人数。
db.students.find({major:”计算机应用技术”}).count()
(5.0)
27、查找所有学生信息,按照专业升序,年龄降序排列。
db.students.find().sort({major:1,age:-1})
(5.0)