在Windows如何以mongorestore來匯入MongoDB dump檔案。
範例環境:
- Windows 64 Bit
- MongoDB v4.0.11
注意mongorestore是一支獨立工具程式,而非mongo shell指令,所以是直接在命令列cmd進行操作。
mongorestore位在MongoDB安裝目錄下的bin資料夾,所以開啟命令列並將目錄移到
C:\Program Files\MongoDB\Server\4.0\bin。以下皆在此位置操作mongorestore。
mongorestore的語法為mongorestore [options] [<directory>/<BSON file>]。
[options]為選填的參數,例如--host,--db,--collection等。
[<directory>/<BSON file>]為要匯入的dump file目錄或bson檔案。
mongorestore若不指定host,預設以localhost:27017的MongoDB為匯入位置。
如要指定匯入的資料庫使用--db=<database>參數。<database>是要匯入的資料庫名稱,例如要匯入的資料庫名稱為mydb則使用參數--db=mydb。
例如下面指令把先前透過mongodump指令dump至D:\mydump\mydb目錄的檔案匯回本機MongoDB的mydb資料庫。
C:\Program Files\MongoDB\Server\4.0\bin>mongorestore --host=localhost:27017 --db=mydb D:\mydump\mydb
2020-01-26T14:47:32.070+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will
not exist in the future; use --nsInclude instead
2020-01-26T14:47:32.075+0800 building a list of collections to restore from D:\mydump\mydb dir
2020-01-26T14:47:32.126+0800 reading metadata for mydb.customers from D:\mydump\mydb\customers.metadata.json
2020-01-26T14:47:32.306+0800 restoring mydb.customers from D:\mydump\mydb\customers.bson
2020-01-26T14:47:32.422+0800 no indexes to restore
2020-01-26T14:47:32.422+0800 finished restoring mydb.customers (2 documents)
2020-01-26T14:47:32.423+0800 done
參考:
沒有留言:
張貼留言