Leon's Blogging

Coding blogging for hackers.

Mongodb Basic

| Comments

mongo 基本操作

刪除 db

1
mongo <dbname> --eval "db.dropDatabase()"

顯示 db

1
show dbs

進去 db

1
use <dbname>

顯示 collections

1
show collections

查詢

1
db.<collectionNmae>.find()

備份 db

1
2
3
4
5
6
mongodump -h 127.0.0.1 -d my-mongo -o ./mongo-backup

# -h: 要備份的 MongoDB 連線位置
# -d: 要備份的 Database 名稱
# -u: 資料庫使用者名稱
# -p: 資料庫密碼

還原 db

1
2
3
4
5
6
7
8
mongorestore -h 127.0.0.1 -d my-mongo-new --directoryperdb ./mongo-backup/my-mongo

# -h: 要還原的 MongoDB 連線位置
# -d: 要還原的 Database 名稱
# -u: 資料庫使用者名稱
# -p: 資料庫密碼
# --directoryperdb: 指定要還原的資料庫檔案來源目錄名稱
# --drop: 如果資料庫存在就刪除重新建立 (小心使用)

Comments