Mac環(huán)境下安裝使用MongoDB

Mongodb相比于MySQL、Oracle、Redis是最接近前端的數(shù)據(jù)庫(kù),是一個(gè)非關(guān)系型的數(shù)據(jù)庫(kù),文檔存儲(chǔ)形式都是以JSON的形式存入的。

一、下載MongoDB數(shù)據(jù)庫(kù)并進(jìn)行安裝

下載地址:https://www.mongodb.com/try/download/community

下載示例.png

將MongoDB解壓到某個(gè)目錄下,我這邊放在文檔Documents的目錄下邊。

二、創(chuàng)建軟鏈接

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

三、創(chuàng)建配置文件

配置文件.png

mongo.conf配置內(nèi)容如下:

# 數(shù)據(jù)庫(kù)路徑
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
# 錯(cuò)誤日志采用追加模式
logappend=true
# 啟用日志文件,默認(rèn)啟用
journal=true
# 過(guò)濾一些無(wú)用的日志信息,若需要調(diào)試設(shè)置為false
# quite=true
# 端口號(hào) 默認(rèn)為27017
port=27017
# 是否需要校驗(yàn),測(cè)試環(huán)境可以關(guān)閉,生產(chǎn)環(huán)境則需要打開(kāi)
# auth=true
# 注冊(cè)服務(wù),這樣就可以保證電腦啟動(dòng)服務(wù)就可以使用,避免每次關(guān)閉后還需要重新啟動(dòng)服務(wù)
fork=true

四、啟動(dòng)服務(wù)

mongod --config /Users/hanqiao/Documents/mongodb-macos-x86_64-5.0.9/mongo/conf/mongo.conf

此時(shí)出現(xiàn)一下內(nèi)容則啟動(dòng)成功

about to fork child process, waiting until server is ready for connections.
forked process: 69854
child process started successfully, parent exiting

此時(shí)輸入命令:mongo就進(jìn)入了命令行中的編輯區(qū)

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常用語(yǔ)法

(1)SQL與MongoDB對(duì)比
SQL MongoDB
表(Table) 集合(Collection)
行(Row) 文檔(Document)
列(Col) 字段(Field)
主鍵(Primary Key) 對(duì)象ID(ObjectId)
(2)數(shù)據(jù)庫(kù)操作

創(chuàng)建數(shù)據(jù)庫(kù):use demo
查看數(shù)據(jù)庫(kù):show dbs
刪除數(shù)據(jù)庫(kù):db.dropDatabase()

(3)集合(表)操作

創(chuàng)建集合:db.createCollection(name)
查看集合:show collections
刪除集合:db.collection.drop()

(4)文檔操作

創(chuàng)建文檔: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

具體示例:

# 查看數(shù)據(jù)庫(kù)
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
# 創(chuàng)建數(shù)據(jù)庫(kù)
> use demo
switched to db demo
# 創(chuàng)建集合
> db.createCollection("users")
{ "ok" : 1 }
# 查看集合
> show collections
users
# 創(chuàng)建一個(gè)空文檔
> db.users.insertOne({})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("62d917201c77c8032f399201")
}
# 創(chuàng)建文檔
> db.users.insertOne({userId: 1, userName:'Han Qiao', age: 30, score:300})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("62d9173f1c77c8032f399202")
}
# 創(chuàng)建多條數(shù)據(jù)文檔
> 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 }
# 刪除一個(gè)文檔
> 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,且全部進(jìn)行更新
> 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數(shù)據(jù)庫(kù)


連接MongoDB數(shù)據(jù)庫(kù).png
連接MongoDB數(shù)據(jù)庫(kù)2.png
連接MongoDB數(shù)據(jù)庫(kù)3.png
查看操作.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 項(xiàng)目的技術(shù)架構(gòu) 1.1 技術(shù)架構(gòu) 學(xué)成在線采用當(dāng)前流行的前后端分離架構(gòu)開(kāi)發(fā),由用戶層、UI層、微服務(wù)層、數(shù)據(jù)...
    彈鋼琴的崽崽閱讀 1,011評(píng)論 1 2
  • MongoDB是一個(gè)C++編寫(xiě)的基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),是一個(gè)介于關(guān)系和非關(guān)系之間的數(shù)據(jù)庫(kù),當(dāng)然也屬于NoSQ...
    Anwar_ec28閱讀 5,068評(píng)論 0 0
  • mac 安裝 mongodb 安裝 官網(wǎng)下載mongodb 官網(wǎng):https://www.mongodb.com/...
    正為瘋狂閱讀 1,889評(píng)論 0 3
  • MongoDB 1. MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫(xiě)。旨在為 WEB 應(yīng)用...
    Kevinr閱讀 1,717評(píng)論 0 3
  • MongoDB 是由 C++ 語(yǔ)言編寫(xiě)的非關(guān)系型數(shù)據(jù)庫(kù),是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),其內(nèi)容存儲(chǔ)形式類...
    何苦_python_java閱讀 752評(píng)論 0 2

友情鏈接更多精彩內(nèi)容