[关闭]
@yangwenbo 2023-03-01T16:21:11.000000Z 字数 3984 阅读 288

ubuntu系统

Ubuntu 22.04 安装Mongodb6.X

1、Mongodb简介

1.1 什么是MongoDB?

Mongodb是一个跨平台的面向文档的NoSQL数据库。它使用带有可选模式的类似JSON的BSON来存储数据。应用程序可以以JSON格式检索信息。

1.2 MongoDB的优点

  • 可以快速开发web型应用,因为灵活,不用像关系型数据库一样需要建表
  • MongoDB存储的是文档(document),文档内存储的是类似json的结构,所谓json就是字符串数组

1.3 MongoDB的数据库分类

  • 数据库(database):用来存储集合的,而且数据库也有分大小
  • 集合(collection):集合类似于数组,用于存放文档的
  • 文档(document):文档是MongoDB数据库中最小的单位

image.png-169.6kB

MongoDB关系: 数据库(database) > 集合(collection)> 文档(document)

2、安装mongodb

2.1 导入MongoDB6.0版的公钥

  1. root@Mongodb:~# curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

2.2 更新apt资源库

  1. root@Mongodb:~# apt update

2.3 创建列表文件

  1. root@Mongodb:~# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  2. root@Mongodb:~# apt update

2.4 安装MongoDB的依赖libssl1.1

  1. root@Mongodb:~# curl -LO http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1-1ubuntu2.1~18.04.21_amd64.deb
  2. root@Mongodb:~# dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.21_amd64.deb

2.5 安装mongodb

  1. root@Mongodb:~# apt-get install -y mongodb-org

3、启动MongoDB服务

  1. #启动MongoDB服务
  2. root@Mongodb:~# systemctl start mongod
  3. #检查MongoDB服务状态
  4. root@Mongodb:~# systemctl status mongod |grep active
  5. Active: active (running) since Tue 2023-02-21 16:54:50 CST; 10s ago
  6. #设置服务开机自启动
  7. root@Mongodb:~# systemctl enable mongod

4、MongoDB的使用

4.1 进入MongoDB

因为版本是6.0,所以需要在终端输入mongosh,该命令相当于6.0版本之前的mongo命令

  1. root@Mongodb:~# mongosh
  2. Current Mongosh Log ID: 63f48e2e5d50ed0f2ed35d3c
  3. Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.7.1
  4. Using MongoDB: 6.0.4
  5. Using Mongosh: 1.7.1
  6. For mongosh info see: https://docs.mongodb.com/mongodb-shell/
  7. To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
  8. You can opt-out by running the disableTelemetry() command.
  9. ------
  10. The server generated these startup warnings when booting
  11. 2023-02-21T16:54:50.226+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
  12. 2023-02-21T16:54:50.700+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
  13. 2023-02-21T16:54:50.700+08:00: vm.max_map_count is too low
  14. ------
  15. ------
  16. Enable MongoDB's free cloud-based monitoring service, which will then receive and display
  17. metrics about your deployment (disk utilization, CPU, operation statistics, etc).
  18. The monitoring data will be available on a MongoDB website with a unique URL accessible to you
  19. and anyone you share the URL with. MongoDB may use this information to make product
  20. improvements and to suggest MongoDB products and deployment options to you.
  21. To enable free monitoring, run the following command: db.enableFreeMonitoring()
  22. To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
  23. ------
  24. test>

4.2 查看MongoDB数据库中的所有数据库

  1. test> show dbs
  2. admin 40.00 KiB
  3. config 12.00 KiB
  4. local 40.00 KiB

4.3 打开或者新建一个数据库

MongoDB不需要预先创建文档,在使用时自动创建

  1. test> use fib
  2. switched to db fib
  3. fib>

4.4 添加集合

集合相当于mysql数据库中的表

  1. fib> db.createCollection('teacher')
  2. { ok: 1 }
  3. fib> show collections
  4. teacher

4.5 插入数据

  1. fib> db.teacher.insert({_id:1,sname:'张三',sage:20})
  2. DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
  3. { acknowledged: true, insertedIds: { '0': 1 } }

4.6 查询所有记录

  1. fib> db.teacher.find()
  2. [ { _id: 1, sname: '张三', sage: 20 } ]

4.7 更新操作

  1. fib> db.teacher.update({_id:1},{$set:{sname:'李四'}})
  2. DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.
  3. {
  4. acknowledged: true,
  5. insertedId: null,
  6. matchedCount: 1,
  7. modifiedCount: 1,
  8. upsertedCount: 0
  9. }

4.8 查询

4.8.1 查询sname='李四'的记录
  1. fib> db.teacher.find({sname:'李四'})
  2. [ { _id: 1, sname: '李四', sage: 20 } ]
4.8.2 查询指定列sname数据
  1. fib> db.teacher.find({},{sname:1})
  2. [ { _id: 1, sname: '李四' } ]
4.8.3 AND条件查询
  1. fib> db.teacher.find({sname:'李四',sage:20})
  2. [ { _id: 1, sname: '李四', sage: 20 } ]
4.8.4 OR条件查询
  1. fib> db.teacher.find({$or:[{sage:20},{sage:21}]})
  2. [ { _id: 1, sname: '李四', sage: 20 } ]
4.8.5 格式化输出
  1. fib> db.teacher.find().pretty()
  2. [ { _id: 1, sname: '李四', sage: 20 } ]

4.9 删除

4.9.1 删除数据
  1. fib> db.teacher.remove({sname:'李四'})
  2. DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.
  3. { acknowledged: true, deletedCount: 1 }
4.9.2 删除集合
  1. fib> db.teacher.drop()
  2. true
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注