- 创建一个学生信息表
use students
2.插入十五条数据
db.students.insertMany(
[
{name:"bob", age:16,sex:"male",grade:95},
{name:"ahn", age:18,sex:"female",grade:45},
{name:"xi", age:15,sex:"male",grade:75},
{name:"bob1", age:16,sex:"male",grade:95},
{name:"ahn1", age:18,sex:"female",grade:45},
{name:"xi1", age:15,sex:"male",grade:55},
{name:"bob2", age:16,sex:"male",grade:95},
{name:"ahn2", age:18,sex:"female",grade:60},
{name:"xi2", age:15,sex:"male",grade:75},
{name:"bob2", age:16,sex:"male",grade:95},
{name:"ahn2", age:18,sex:"female",grade:45},
{name:"xi2", age:15,sex:"male",grade:85},
{name:"bob2", age:16,sex:"male",grade:95},
{name:"ahn2", age:18,sex:"female",grade:45},
{name:"xi2", age:15,sex:"male",grade:75},
]
)
- 查询所有的男生数据(只需要学生的姓名和年龄)
db.students.find({sex:"male"},{name:1, age:true, _id:0})
- 查询成绩几个的学生信息
db.students.find({grade:{"$gte":60}})
- 查询所有16岁的男生和18岁的女生的数据
db.students.find({$or:[{sex:"male",age:16},{sex:"female",age:18}]})
- 按照学生的年龄进行排序
db.students.find().sort({age:1})
db.students.find().sort({age:-1})
- 将所有的学生年龄增加一岁
db.students.update({},{"$inc":{age:1}},{multi:true})
- 将所有的女学生年龄增加一岁
db.students.update({sex:"female"},{"$inc":{age:1}},{multi:true})
- 将所有的学生年龄增加一个address属性
db.students.update({},{"$set":{address:"--"}},{multi:true})