1.mongoexport工具
mongoexport工具,即:mongoexport.exe
位於mongo/bin目錄下:
2.使用
在cmd.exe中進入安裝mongo的bin目錄,比如我的:cd D:\mongoDB\mongoDB\bin
在此目錄下就可以使用mongoexport的命令進行數據導出操作了。
例如我們可以查看mongoexport的命令使用參數,在bin目錄下輸入:mongoexport --help (注意是兩個"-")
我們可以看到具體的參數解釋,這裏簡要說明一下:
參數說明:
-h:指明數據庫宿主機的IP
-u:指明數據庫的用戶名
-p:指明數據庫的密碼
-d:指明數據庫的名字
-c:指明collection的名字
-f:指明要導出那些列
-o:指明到要導出的文件名
-q:指明導出數據的過濾條件
3.複雜條件查詢
我們在可視化工具中進行較複雜的查詢時,一般這樣寫:
db.getCollection('rungps').find({"user_id":"98805835","sport_id":"989292c0"})
查詢條件爲一個json:{"user_id":"98805835","sport_id":"989292c0"}。我們注意到上面的參數 -q(指明導出數據的過濾條件),我們在-q參數後面可以跟一個json,作爲導出數據的過濾條件。但是我在導出過程中出現了問題。
我的語句1:
mongoexport -h xxx --port xx -u xx -p xx -d xx -c xx --type=json -q {"user_id":"98805835"} -o D:\mongoDataJson\run.json
報錯:
我的語句2:mongoexport -h xxx --port xx -u xx -p xx -d xx -c xx --type=json -q{"user_id":"98805835","sport_id":"989292c0xx"} -out D:\mongoDataJson\run.json
報錯:
出現問題的原因,可能是json在shell(cmd.exe)中格式不對,因此我採用讀取文件中的json的方式。
我在路徑D:\mongoDataJson中創建txt文件:query.txt。在裏面寫入json:
執行語句:
mongoexport -h xx --port xx -u xx -p xx -d xx -c xx --type=json --queryFile D:\mongoDataJson\query.txt -o D:\mongoDataJson\run.json
完成導入數據到run.json的任務。