- 安装MongoDB(Ubuntu版本:22.04 LTS)
0.查看Ubuntu版本
命令:lsb_release -a
1.使用Ubuntu命令安装
(1)更新系统包:
sudo apt update(可选)
sudo apt upgrade(可选)
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
(2)安装MongoDB的依赖libssl1.1(安装curl后使用curl命令安装)
(3)运行下命令将 MongoDB 存储库添加到的 Ubuntu 系统(可选?)
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
(4)添加MongoDB GPG密钥
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
(5)添加储存库,同时命令安装MongoDB服务包
sudo apt update
sudo apt install mongodb-org(sudo apt install -y mongodb-org)
2.启动MongoDB,验证状态
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod
3.测试MongoDB
mongo --eval 'db.runCommand({connectionStatus: 1})'
4.启动MongoDB Shell
mongo
- 根据给出的Student文档完成操作
Student文档:
{
“name”: “zhangsan”,
“score”:{
“English”: 70,
“Math”: 80,
“Computer”: 90
}
}
{
“name”: “lisi”,
“score”: {
“English”: 30,
“Math”: 40,
“Computer”: 50
}
}
1.用 MongoDB Shell 设计出 Student 集合。
(1)创建数据库:(数据库名字改成自己的!)
Use database_liuhao
(2)创建集合:
db.createCollection(“Student”)
(3)插入数据:
db.Student.insert( name:”zhangsan”, score:{ English:70, Math:80, Computer:90 } })
db.Student.insert( name:”lisi”, score:{ English:30, Math:40, Computer:50 } })
2.用 find()方法输出两个学生的信息。
db.Student.find()
db.Student.find().pretty() //易读方式
3.用 find()方法查询 lisi 的所有成绩(只显示 score 列)。
db.Student.find({ name:”lisi” }, { score:1, _id:0 })
4.修改 zhangsan 的 Math 成绩为 66。
db.Student.update({name:”zhangsan”},{$set:{“score.Math”:66}})
MongoDB官网:MongoDB Documentation
MongoDB教程1:MongoDB 创建数据库_w3cschool
MongoDB教程2:MongoDB 教程 | 菜鸟教程
MongoDB嵌套修改:MongoDB 如何更新嵌套文档? - 知乎