下载安装后
1.在 “你自己MongoDB安装目录/bin” 下按Shift+鼠标右键后选择打开PowerShell(需要输入.\)或cmd(不需要输入.\)
2.输入[.\]mongod --dbpath XXX(任意指定但最好在你自己MongoDB安装目录\data下)确认后就打开了MongoDB的服务端
3.再打开一个PowerShell或cmd输入:[.\]mongo就打开了MongoDB的客户端(也不知道啥情况,有时候连接服务端的时候还要去服务端随便按点什么)
1.展示数据库里的表
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
2.使用某个数据库
> use students
switched to db students
这里可以直接选择尚不存在的数据库,如果有写入等操作后,表(文档)会被直接创建
3.查看正在使用的数据库
> db
students
4. 创建一个对象(
> stu = {
... name: 'Jhon',
... age: 21}
{ "name" : "Jhon", "age" : 21 }
5.把对象stu插入到表students中去
> db.students.insert(stu)
WriteResult({ "nInserted" : 1 })
6.查询students中的数据
> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "Jhon", "age" : 21 }
其中的_id是MongoDB自动创建并维护的类似主键的字段
7.在不建立对象的情况下插入数据
> db.students.insert({name: 'Amy'})
WriteResult({ "nInserted" : 1 })
8.查询一条数据
> db.students.findOne()
{
"_id" : ObjectId("5c7e585576ba887d0e8629d0"),
"name" : "Jhon",
"age" : 21
}
数据被格式化了
9.修改某条数据
> obj = db.students.findOne()
{
"_id" : ObjectId("5c7e585576ba887d0e8629d0"),
"name" : "Jhon",
"age" : 21
}
> obj.name = "lux"
lux
> db.students.update({name:'Jhon'}, obj)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.findOne()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }
先把某条数据查询出来赋值给一个变量,
然后改变变量的一条属性,
然后更新满足条件的记录。(好傻)
为啥最后一句的findOne不格式化了??(⊙_⊙)?
10.删除数据
> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }
{ "_id" : ObjectId("5c7e598176ba887d0e8629d1"), "name" : "Amy" }
> db.students.remove({name: 'Amy'})
WriteResult({ "nRemoved" : 1 })
> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }
先查询看到有两条数据,
执行删除满足条件的记录,
再查询看看是少了一条
11.清空表
> db.students.remove({})
WriteResult({ "nRemoved" : 1 })
> db.students.find()
>
从删库到跑路
任务
1.创建一个学生信息表(至少包含:姓名,性别,成绩,年龄)
2.写入十五条不同的数据
3.查询所有的男生数据(只需要学生的年龄和姓名)
4.查询成绩及格的学生的信息
5.查询所有18岁的男生和16岁女生的数据
6.按照年龄对学生进行排序
7.将所有的女学生年龄增加一岁
练习
1.创建一个学生信息表(至少包含:姓名,性别,成绩,年龄)
2.写入十五条不同的数据
> db.students.insertMany(
... [
... { name: 'bob', age: 16, sex: 'male', grade: 95},
... { name: 'lulu', age: 16, sex: 'female', grade: 50},
... { name: 'san', age: 18, sex: 'male', grade: 80},
... { name: 'mac', age: 16, sex: 'male', grade: 60},
... { name: 'sli', age: 18, sex: 'female', grade: 70},
... { name: 'tom', age: 20, sex: 'male', grade: 90},
... { name: 'tim', age: 17, sex: 'male', grade: 50},
... { name: 'ann', age: 80, sex: 'female', grade: 60},
... { name: 'joy', age: 45, sex: 'female', grade: 45},
... { name: 'wm', age: 13, sex: 'female', grade: 60},
... { name: 'ty', age: 14, sex: 'female', grade: 70},
... { name: 'lu', age: 15, sex: 'male', grade: 75},
... { name: 'le', age: 16, sex: 'male', grade: 80},
... { name: 'jo', age: 14, sex: 'male', grade: 75},
... { name: 'lce', age: 16, sex: 'female', grade: 85},
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5c7e689f76ba887d0e8629df"),
ObjectId("5c7e689f76ba887d0e8629e0"),
ObjectId("5c7e689f76ba887d0e8629e1"),
ObjectId("5c7e689f76ba887d0e8629e2"),
ObjectId("5c7e689f76ba887d0e8629e3"),
ObjectId("5c7e689f76ba887d0e8629e4"),
ObjectId("5c7e689f76ba887d0e8629e5"),
ObjectId("5c7e689f76ba887d0e8629e6"),
ObjectId("5c7e689f76ba887d0e8629e7"),
ObjectId("5c7e689f76ba887d0e8629e8"),
ObjectId("5c7e689f76ba887d0e8629e9"),
ObjectId("5c7e689f76ba887d0e8629ea"),
ObjectId("5c7e689f76ba887d0e8629eb"),
ObjectId("5c7e689f76ba887d0e8629ec"),
ObjectId("5c7e689f76ba887d0e8629ed")
]
}
> db.students.find()
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e7"), "name" : "joy", "age" : 45, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }
为了方便这里写一个文本文件
db.students.insertMany(
[
{ name: 'bob', age: 16, sex: 'male', grade: 95},
{ name: 'lulu', age: 16, sex: 'female', grade: 50},
{ name: 'san', age: 18, sex: 'male', grade: 80},
{ name: 'mac', age: 16, sex: 'male', grade: 60},
{ name: 'sli', age: 18, sex: 'female', grade: 70},
{ name: 'tom', age: 20, sex: 'male', grade: 90},
{ name: 'tim', age: 17, sex: 'male', grade: 50},
{ name: 'ann', age: 80, sex: 'female', grade: 60},
{ name: 'joy', age: 45, sex: 'female', grade: 45},
{ name: 'wm', age: 13, sex: 'female', grade: 60},
{ name: 'ty', age: 14, sex: 'female', grade: 70},
{ name: 'lu', age: 15, sex: 'male', grade: 75},
{ name: 'le', age: 16, sex: 'male', grade: 80},
{ name: 'jo', age: 14, sex: 'male', grade: 75},
{ name: 'lce', age: 16, sex: 'female', grade: 85},
]
)
3.查询所有的男生数据(只需要学生的年龄和姓名)
> db.students.find({sex: 'male'})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
但这不符合题目要求
> db.students.find({sex: 'male'},{name: 1, age: true, _id: 0})
{ "name" : "bob", "age" : 16 }
{ "name" : "san", "age" : 18 }
{ "name" : "mac", "age" : 16 }
{ "name" : "tom", "age" : 20 }
{ "name" : "tim", "age" : 17 }
{ "name" : "lu", "age" : 15 }
{ "name" : "le", "age" : 16 }
{ "name" : "jo", "age" : 14 }
其中name后的1和age后的true是等价的,0和false是等价的
4.查询及格的学生的信息
> db.students.find({grade: {'$gte': 60}})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }
5.查询所有18岁的男生和16岁女生的数据
> db.students.find({'$or': [{sex: 'male', age: 18}, {sex: 'female', age: 16}]})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }
6.按照年龄对学生进行排序
> db.students.find().sort({age: 1})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e7"), "name" : "joy", "age" : 45, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }
age后面那个1代表升序,如果是 -1代表降序
7.将所有的女学生年龄增加一岁
> db.students.update({sex: 'female'}, {'$inc': {age: 1}}, {multi: true})
WriteResult({ "nMatched" : 15, "nUpserted" : 0, "nModified" : 15 })
这里用到了修改器,其中 '$inc': {age: 1}是指age增加1,multi=true指多条数据
'$set': {XXX: XXX }可以用来新增属性
网友评论