Mac环境下安装使用MongoDB

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"
    }
}

三、创建配置文件

配置文件.png

mongo.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

Studio 3T下载

图片.png

下载后安装,连接MongoDB数据库


连接MongoDB数据库.png
连接MongoDB数据库2.png
连接MongoDB数据库3.png
查看操作.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容