如果你正在尋找一種現代的、高效的NoSQL文檔數據庫,那麼MongoDB可能是你想要的。它的管理容易、可擴展性高,最重要的是,對於開發人員來說,MongoDB比許多其它數據庫更加友好。
一、下載MongoDB並安裝至本地計算機
首先,你需要訪問官方網站下載MongoDB的最新版本。點擊下載鏈接之後,選擇對應的安裝包下載,下載完成後,運行安裝包開始安裝。
1、選擇默認安裝路徑
C:\Program Files\MongoDB\
2、添加MongoDB的bin目錄至環境變量中,方便在命令行中使用MongoDB
C:\Program Files\MongoDB\Server\4.4\bin\;
二、配置MongoDB
在安裝MongoDB之後,你需要進行一些配置
1、創建數據存儲目錄
md C:\data\db
2、啟動MongoDB服務
mongod
現在你已經啟動了MongoDB服務,接下來需要使用Mongo Shell來對數據庫進行管理。
三、使用Mongo Shell連接MongoDB
Mongo Shell是MongoDB的命令行工具,可以用它連接MongoDB並對數據進行操作。
1、啟動Mongo Shell
mongo
啟動完後,你應該可以看到以下類似的輸出:
MongoDB shell version v4.4.4 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("a45e9305-032d-446c-a1de-d5796d7a85ef") } MongoDB server version: 4.4.4 --- The server generated these startup warnings when booting: 2021-03-01T09:14:23.172+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2021-03-01T09:14:23.173+08:00: You are running this process as the root user, which is not recommended 2021-03-01T09:14:23.173+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip 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 2021-03-01T09:14:23.178+08:00: Listening on all addresses 2021-03-01T09:14:23.179+08:00: Listening on port 27017 2021-03-01T09:14:23.179+08:00: warning: bind_ip of 127.0.0.1 is unnecessary; listens on all ips by default --- >
2、查看數據庫列表
show dbs
這將列出你連接到的MongoDB實例上所有的數據庫,如下所示:
admin 0.000GB config 0.000GB local 0.000GB
四、常用命令
MongoDB提供了許多命令,這裡列出了一些常用命令以供參考。
1、創建數據庫
use <database_name>
2、查看當前使用的數據庫
db
3、查看指定數據庫下的所有集合
show collections
4、插入一條數據
db.<collection_name>.insertOne({key1:value1, key2:value2})
5、查詢數據
db.<collection_name>.find()
五、總結
本文已經介紹了Windows MongoDB安裝及配置指南,並列出了一些常用命令以供參考。MongoDB是一個強大的NoSQL文檔數據庫,它易用、擴展性好,值得開發人員深入學習和使用。
原創文章,作者:XZVQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/138628.html