1. 程式人生 > 實用技巧 >MongoDB簡介---MongoDB基礎用法(一)

MongoDB簡介---MongoDB基礎用法(一)

Mongo

MongoDB是一個基於分散式檔案儲存的資料庫。MongoDB是一個介於關係資料庫和非關係資料庫之間的產品,是非關係資料庫當中功能最豐富,最像關係資料庫的。

MongoDB 將資料儲存為一個文件,資料結構由鍵值(key=>value)對組成。MongoDB 文件類似於 JSON 物件。欄位值可以包含其他文件,陣列及文件陣列。

主要特點

  • MongoDB 是一個面向文件儲存的資料庫,操作起來比較簡單和容易。
  • 你可以在MongoDB記錄中設定任何屬性的索引 (如:FirstName="Sameer",Address="8 Gandhi Road")來實現更快的排序。
  • 你可以通過本地或者網路建立資料映象,這使得MongoDB有更強的擴充套件性。
  • 如果負載的增加(需要更多的儲存空間和更強的處理能力) ,它可以分佈在計算機網路中的其他節點上這就是所謂的分片。
  • Mongo支援豐富的查詢表示式。查詢指令使用JSON形式的標記,可輕易查詢文件中內嵌的物件及陣列。
  • MongoDb 使用update()命令可以實現替換完成的文件(資料)或者一些指定的資料欄位 。
  • Mongodb中的Map/reduce主要是用來對資料進行批量處理和聚合操作。
  • Map和Reduce。Map函式呼叫emit(key,value)遍歷集合中所有的記錄,將key與value傳給Reduce函式進行處理。
  • Map函式和Reduce函式是使用Javascript編寫的,並可以通過db.runCommand或mapreduce命令來執行MapReduce操作。
  • GridFS是MongoDB中的一個內建功能,可以用於存放大量小檔案。
  • MongoDB允許在服務端執行指令碼,可以用Javascript編寫某個函式,直接在服務端執行,也可以把函式的定義儲存在服務端,下次直接呼叫即可。
  • MongoDB支援各種程式語言:RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。
  • MongoDB安裝簡單。

CentOS 安裝配置

MongoDB 原始碼下載地址:https://www.mongodb.com/try/download/community

選擇 tgz 下載

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.1.tgz
mv mongodb-linux-x86_64-rhel70-4.4.1/ /usr/local/mongodb

vi /etc/profile
PATH=/usr/local/mongodb/bin:$PATH

source /etc/profile

建立資料庫目錄

預設情況下 MongoDB 啟動後會初始化以下兩個目錄:

  • 資料儲存目錄:/var/lib/mongodb
  • 日誌檔案目錄:/var/log/mongodb

我們在啟動前可以先建立這兩個目錄並設定當前使用者有讀寫許可權:

sudo mkdir -p /var/lib/mongodb
sudo mkdir -p /var/log/mongodb
sudo chown 'current_user' /var/lib/mongo   # 設定許可權
sudo chown 'current_user' /var/log/mongodb  # 設定許可權

建立配置檔案

vi /usr/local/mongodb/mongodb.conf
# mongodb 配置檔案
port=27017  #埠
bind_ip=0.0.0.0 #預設是127.0.0.1
dbpath=/var/lib/mongodb/  #資料庫存放
logpath=/var/log/mongodb/mongodb.log #日誌檔案
fork=true #設定後臺執行
#auth=true #開啟認證

啟動 Mongodb 服務

mongod --config /usr/local/mongodb/mongodb.conf

瀏覽器訪問 IP:27017(這裡是虛擬機器),出現以下介面表示啟動成功。

![image-20201027160147079](file://C:/Users/yanghelong/Desktop/img/img/NoSQL/image-20201027160147079.png?lastModify=1603852614)

mongodb 後臺管理

你需要先開啟 mongodb 裝目錄的下的 bin 目錄,然後執行 mongo 命令檔案。

MongoDB Shell 是 MongoDB 自帶的互動式 Javascript shell,用來對 MongoDB 進行操作和管理的互動式環境。

當你進入 mongoDB 後臺後,它預設會連結到 test 文件(資料庫):

$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0795c040-abef-4fec-90c9-6b9a7a78e3c2") }
MongoDB server version: 4.4.1
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: 
     2020-10-27T03:29:57.206-04:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
     2020-10-27T03:29:57.207-04:00: You are running this process as the root user, which is not recommended
     2020-10-27T03:29:57.207-04: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
     2020-10-27T03:29:57.207-04:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
     2020-10-27T03:29:57.207-04:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
     2020-10-27T03:29:57.207-04:00: Soft rlimits too low
     2020-10-27T03:29:57.207-04:00:     currentValue: 1024
     2020-10-27T03:29:57.207-04: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

#先通過shell連上伺服器:
mongo
use admin
db.shutdownServer()

MongoDB 工具

mongoimport mongoexport mongodump mongorestore 等工具,作為 mongodb database tools 提供了單獨的下載入口

地址 : https://www.mongodb.com/try/download/database-tools

下載後解壓到安裝路徑下的bin目錄中,這裡是 /usr/local/mongodb/bin/

MongoDB 概念解析

SQL術語/概念 MongoDB術語/概念 解釋/說明
database database 資料庫
table collection 資料庫表/集合
row document 資料記錄行/文件
column field 資料欄位/域
index index 索引
table joins 表連線,MongoDB不支援
primary key primary key 主鍵,MongoDB自動將_id欄位設定為主鍵