MongoDb
什么是mongodb?
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。
在mongodb中数据库对应mysql的数据库,集合对应mysql的表,可以这样理解
搭建mongodb环境
下载地址:https://www.mongodb.com/download-center/community
安装完成之后,直接在安装路径下面找到bin里面的mongo.exe,直接执行
出现如图便是成功的
可以执行简单的操作命令试一试,show databases 显示所有数据
mongodb常见的指令:
use DATABASE_NAME 创建数据库,数据库不存在直接创建
db.runoob.insert(json数据) 插入语数据
show collections 展示数据库下面的集合
db.createCollection(集合的名字)
db.collection.drop() 删除集合
use 数据库名字 表示准备使用那一个数据库,切换数据库也是这样的操作
db.collection.find({}) 查询所有
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2c19d0a5-a60f-478f-a64f-48533f0abfb7") }
MongoDB server version: 5.0.2
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-09-15T22:50:18.350+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show database
uncaught exception: Error: don't know how to show [database] :
shellHelper.show@src/mongo/shell/utils.js:1211:11
shellHelper@src/mongo/shell/utils.js:838:15
@(shellhelp2):1:1
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
> db.rrrr.insert({"aaa":1})
WriteResult({ "nInserted" : 1 })
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use test
switched to db test
> show collesctions
uncaught exception: Error: don't know how to show [collesctions] :
shellHelper.show@src/mongo/shell/utils.js:1211:11
shellHelper@src/mongo/shell/utils.js:838:15
@(shellhelp2):1:1
> show collections
rrrr
> db.createCollection("name")
{ "ok" : 1 }
> show collections
name
rrrr
> db.rrrr.drop()
true
> show collections
name
> use admin
switched to db admin
> show collections
system.version
>
mongodb使用条件
- 数据量超过亿级并且还要不断的扩容
- 每秒2000-3000的读写
- 数据莫名多种多样,来源多
- 大型地理位置的使用 (这个大公司使用的多)
- 系统快速写入,低延迟
- 集合之间的关系不要太复杂
mongodb使用场景
- 日志收集
- lot设备
- 数据爬虫