mongodb 安裝 以及 常用命令
阿新 • • 發佈:2020-07-15
1.下載 & 安裝
a)點選 Try Free
b) 選擇本地 -> 社群伺服器 community server
c) 下載解壓後 放入 /usr/local command + shift + g 前往資料夾 輸入路徑
d) 開啟終端,配置環境變數
終端中輸入:
open .bash_profile
開啟的檔案中輸入:
export PATH=${PATH}:/usr/local/mongoDB/bin
command + s 儲存
終端中輸入:
source .bash_profile
使配置立即生效。
e) 終端中輸入 mongo -version 出現下圖中資訊 說明安裝成功
f) 在MongoDB資料夾 同級目錄中建立MongoData資料夾 並在其下 建立 data 和 log 兩個資料夾
用於存放資料和日誌
g) 啟動
mongod --dbpath /usr/local/MongoData
2.常用命令
1)shell中使用
mongo
2) 檢視所有資料庫
show dbs
3) 使用資料庫
use practice
4) 顯示所有集合
show collections
5) 獲取集合名稱列表
db.getCollectionNames()
6) 獲取當前集合中數量
db.collect1.count()
7) 檢視當前集合資料
db.collect1.find()
8) 條件查詢集合中某些資料
db.collect1.find({name: '123'})
9) 建立資料庫
use newDatabase
10) 刪除資料庫
use newDatabase
db.dropDatabase()
11) 建立集合
db.createCollection(name, { size : ..., capped : ..., max : ... } ) 或 db.newCollect.insert({name: 'zhangsan'})
12) 刪除集合
db.newCollect.drop()
13) 插入資料
db.newCollect.insert({name: 'zhangsan'})
14) 刪除資料
db.newCollect.remove({"name":"zhangsan"})