Mongodb相比于MySQL、Oracle、Redis是最接近前端的数据库,是一个非关系型的数据库,文档存储形式都是以JSON的形式存入的。
一、下载MongoDB数据库并进行安装
下载地址:https://www.mongodb.com/try/download/community
下载示例.png将MongoDB解压到某个目录下,我这边放在文档Documents的目录下边。
二、创建软链接
ln -s /Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/bin/mongod /usr/local/bin/mongod
ln -s /Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/bin/mongo /usr/local/bin/mongo
安装完成之后,输入mongo --version
或者mongod --version
有信息输出则安装成功
MongoDB shell version v5.0.9
Build Info: {
"version": "5.0.9",
"gitVersion": "6f7dae919422dcd7f4892c10ff20cdc721ad00e6",
"modules": [],
"allocator": "system",
"environment": {
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
三、创建配置文件
配置文件.pngmongo.conf配置内容如下:
# 数据库路径
dbpath=/Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/mongo/data
# 日志输出文件路径
logpath=/Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/mongo/log/mongo.log
# 错误日志采用追加模式
logappend=true
# 启用日志文件,默认启用
journal=true
# 过滤一些无用的日志信息,若需要调试设置为false
# quite=true
# 端口号 默认为27017
port=27017
# 是否需要校验,测试环境可以关闭,生产环境则需要打开
# auth=true
# 注册服务,这样就可以保证电脑启动服务就可以使用,避免每次关闭后还需要重新启动服务
fork=true
四、启动服务
mongod --config /Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/mongo/conf/mongo.conf
此时出现一下内容则启动成功
about to fork child process, waiting until server is ready for connections.
forked process: 69854
child process started successfully, parent exiting
此时输入命令:mongo
就进入了命令行中的编辑区
MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b488433f-0cae-4560-8d81-0fd4e535a3a2") }
MongoDB server version: 5.0.9
================
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.
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:
2022-07-21T17:04:11.167+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-07-21T17:04:11.168+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2022-07-21T17:04:11.168+08:00: Soft rlimits for open file descriptors too low
2022-07-21T17:04:11.168+08:00: currentValue: 2560
2022-07-21T17:04:11.168+08:00: recommendedMinimum: 64000
---
---
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()
---
>
五、MongoDB常用语法
(1)SQL与MongoDB对比
SQL | MongoDB |
---|---|
表(Table) | 集合(Collection) |
行(Row) | 文档(Document) |
列(Col) | 字段(Field) |
主键(Primary Key) | 对象ID(ObjectId) |
(2)数据库操作
创建数据库:use demo
查看数据库:show dbs
删除数据库:db.dropDatabase()
(3)集合(表)操作
创建集合:db.createCollection(name)
查看集合:show collections
删除集合:db.collection.drop()
(4)文档操作
创建文档:db.collection.insertOne({})
、db.collection.insertMany([])
查看文档:db.collections.find({})
删除文档:db.collection.deleteOne()
、db.collection.deleteMany()
更新文档:db.collection.update({},{},false,true)
(5)条件操作
大于:$gt
小于:$lt
大于等于:$gte
小于等于:$lte
具体示例:
# 查看数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
# 创建数据库
> use demo
switched to db demo
# 创建集合
> db.createCollection("users")
{ "ok" : 1 }
# 查看集合
> show collections
users
# 创建一个空文档
> db.users.insertOne({})
{
"acknowledged" : true,
"insertedId" : ObjectId("62d917201c77c8032f399201")
}
# 创建文档
> db.users.insertOne({userId: 1, userName:'Han Qiao', age: 30, score:300})
{
"acknowledged" : true,
"insertedId" : ObjectId("62d9173f1c77c8032f399202")
}
# 创建多条数据文档
> db.users.insertMany([{userId: 2, userName:'Jack', age: 40, score:400},{userId: 3, userName:'Henry', age: 50, score:200}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62d917951c77c8032f399203"),
ObjectId("62d917951c77c8032f399204")
]
}
# 查看文档
> db.users.find()
{ "_id" : ObjectId("62d917201c77c8032f399201") }
{ "_id" : ObjectId("62d9173f1c77c8032f399202"), "userId" : 1, "userName" : "Han Qiao", "age" : 30, "score" : 300 }
{ "_id" : ObjectId("62d917951c77c8032f399203"), "userId" : 2, "userName" : "Jack", "age" : 40, "score" : 400 }
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 200 }
# 删除一个文档
> db.users.deleteOne({ "_id" : ObjectId("62d917201c77c8032f399201") })
{ "acknowledged" : true, "deletedCount" : 1 }
> db.users.find()
{ "_id" : ObjectId("62d9173f1c77c8032f399202"), "userId" : 1, "userName" : "Han Qiao", "age" : 30, "score" : 300 }
{ "_id" : ObjectId("62d917951c77c8032f399203"), "userId" : 2, "userName" : "Jack", "age" : 40, "score" : 400 }
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 200 }
# 按照条件userid为3查找
> db.users.find({userId:3})
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 200 }
# 按照条件age大于35查找
> db.users.find({age:{$gt:35}})
{ "_id" : ObjectId("62d917951c77c8032f399203"), "userId" : 2, "userName" : "Jack", "age" : 40, "score" : 400 }
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 200 }
# 更新文档,将age大于45的score更新为600
> db.users.update({age:{$gt:45}},{$set:{score:600}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find()
{ "_id" : ObjectId("62d9173f1c77c8032f399202"), "userId" : 1, "userName" : "Han Qiao", "age" : 30, "score" : 300 }
{ "_id" : ObjectId("62d917951c77c8032f399203"), "userId" : 2, "userName" : "Jack", "age" : 40, "score" : 400 }
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 600 }
# 更新文档,将age大于35的score更新为700,且全部进行更新
> db.users.update({age:{$gt:35}},{$set:{score:700}}, false, true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.users.find()
{ "_id" : ObjectId("62d9173f1c77c8032f399202"), "userId" : 1, "userName" : "Han Qiao", "age" : 30, "score" : 300 }
{ "_id" : ObjectId("62d917951c77c8032f399203"), "userId" : 2, "userName" : "Jack", "age" : 40, "score" : 700 }
{ "_id" : ObjectId("62d917951c77c8032f399204"), "userId" : 3, "userName" : "Henry", "age" : 50, "score" : 700 }
>
命令行使用示例.png
六、可视化操作工具Studio 3T
图片.png下载后安装,连接MongoDB数据库
连接MongoDB数据库.png 连接MongoDB数据库2.png 连接MongoDB数据库3.png 查看操作.png
网友评论