建立集合college
var col=[{
"_id" : ObjectId("5941f2bac1bc86928f4de49a"),
"part" : "图书馆",
"loc" : "南望山"
},
{
"_id" : ObjectId("5941f2bac1bc86928f4de49b"),
"part" : "计算机学院",
"loc" : "未来城"
},
{
"_id" : ObjectId("5941f2bac1bc86928f4de49c"),
"part" : "珠宝学院",
"loc" : "南望山"
},
{
"_id" : ObjectId("5941f2bac1bc86928f4de49d"),
"part" : "经管学院",
"loc" : "未来城"
},
]
db.college.insert(col)
建立集合student:
var stu=[{
"_id" : ObjectId("5941f5bfc1bc86928f4de4ac"),
"sname" : "林冲",
"gender" : "男",
"age" : 18,
"tel" : "13810001000",
"major":"计算机学院",
"location":{
"province":"湖北",
"city":"武汉"
}
},
{
"_id" : ObjectId("5941f5bfc1bc86928f4de4ad"),
"sname" : "孙二娘",
"gender" : "女",
"age" : 19,
"tel" : "13812221222",
"major":"珠宝学院",
"location":{
"province":"河南",
"city":"郑州"
}
},
{
"_id" : ObjectId("5941f5bfc1bc86928f4de4ae"),
"sname" : "扈三娘",
"gender" : "女",
"age" : 20,
"tel" : "13813331333",
"major":"经管学院",
"location":{
"province":"湖南",
"city":"长沙"
}
},
]
db.student.insert(stu)
向student集合插入文档:
var stu1=[{
"_id" : ObjectId("5941f5bfc1bc86928f4ae4de"),
"sname" : "扈四娘",
"gender" : "男",
"age" : 20,
"tel" : "13814441444",
"major":"经管学院",
}
]
db.student.insert(stu1)
查询student集合中的文档,并统计文档数量:
db.student.find(),
db.student.count()
查询sname为扈四娘的文档,添加一个location属性:
db.student.find({“sname”:”扈四娘”})
db.student.update({ "_id" : ObjectId("5941f5bfc1bc86928f4ae4de")},
{"$set":{"location":{
"province":"四川",
"city":"重庆"
}
}});
使用{sname:“李白”} 替换 sname 为扈四娘的文档
db.student.update({ "_id" : ObjectId("5941f5bfc1bc86928f4ae4de")},
{" $set":{"sname":"李白" }});
查询age>=19的学生信息
db.student.find({age : {'$gte': 19 }})
查询每个学生所在校区(未来城、南望山)
db.college.find({},{"loc":1})