【MongoDB】MongoDB導出EXCEL格式數據

1、概述

本篇文章主要用來講述如何將MongoDB中的數據導出到EXCEL中。

下面列一下要點:

  • mongoexport使用
  • CSV文件中,中文亂碼
  • CSV文件轉EXCEL文件
  • 條件過濾導出數據

2、本地安裝MongoDB

2.1、下載MongoDB

https://www.mongodb.com/download-center#community

2.2、安裝MongoDB

按照默認的選項一步步安裝即可。

2.3、使用mongoexport導出數據到CSV文件

執行以下命令導出數據

打開終端窗口,切換到MongoDB的安裝目錄下的bin目錄,然後執行以下命令:

mongoexport -h xxxx -u xxxx -p xxxx --authenticationDatabase=admin --authenticationMechanism=SCRAM-SHA-1 --port 27017 -d xxxx -c xxxx --type=csv -f _id,name,mobile,show_count,last_login_at,active -o ./company.csv

參數說明

  • -h:指明數據庫宿主機的IP
  • -u:指明數據庫的用戶名
  • -p:指明數據庫的密碼
  • --port:指明端口號
  • -d:指明數據庫的名字
  • -c:指明collection的名字
  • --type:指明要導入的文件格式
  • -f:指明要導出那些列
  • -q:指明導出數據的過濾條件
  • -o:指明要導出的文件名
  • --authenticationDatabase:指明保存用戶憑證的數據庫
  • --authenticationMechanism:指明身份驗證機制

前置條件

  • 需要使用者獲取MongoDB服務器IP、端口號、登錄名、登錄密碼
  • 本篇文章所用的MongoDB數據庫名稱爲account_service
  • 本篇文章所用的MongoDB數據庫集合名稱爲company
  • 本篇文章所使用的company集合的結構如下:
{
    "_id" : ObjectId,
    "source" : String,
    "name" : String,
    "mobile" : String,
    "password" : String,
    "license_no" : String,
    "active" : String,
    "xd_active" : String,
    "frw_active" : String,
    "allow_borrow" : String,
    "merchant_id" : Array,
    "last_login_ip" : String,
    "last_login_at" : String,
    "updated_at" : Date,
    "created_at" : Date
    "ec_customer_id" : String
}

3、CSV中文亂碼

通過上面的步驟,我們已經可以導出MongoDB數據到CSV中,當我們用EXCEL打開CSV文件時,發現中文亂碼。

中文亂碼解決方案

使用“記事本”打開CSV文件,執行“另存爲”命令,在“另存爲”窗口,選擇“編碼”爲“UTF-8”。打開新保存的CSV就不會出現中文亂碼了。

4、將CSV文件轉爲EXCEL文件

EXCEL可以正常打開CSV文件,也可以另存爲標準EXCEL文檔格式。

四、查詢(高級導出)

4.1、MongoDB與MySQL簡單查詢對比

操作 MongoDB格式 MongoDB示例 MySQL示例
等於 {<key>:<value>} db.getCollection('company').find({"source":"3"}) where source = '3'
小於 {<key>:{$lt:<value>}} db.getCollection('company').find({"last_login_at":{$lt:"2018-07-10 17:08:05"}})) where last_login_at < "2018-07-10 17:08:05"
大於 {<key>:{$gt:<value>}} db.getCollection('company').find({"last_login_at":{$gt:"2018-07-10 17:08:05"}})) where last_login_at > "2018-07-10 17:08:05"
小於或等於 {<key>:{$lte:<value>}} db.getCollection('company').find({"last_login_at":{$lte:"2018-07-10 17:08:05"}})) where last_login_at <= "2018-07-10 17:08:05"
大於或等於 {<key>:{$gte:<value>}} db.getCollection('company').find({"last_login_at":{$gte:"2018-07-10 17:08:05"}})) where last_login_at >= "2018-07-10 17:08:05"
不等於 {<key>:{$ne:<value>}} db.getCollection('company').find({"source":{$ne:"3"}}) where source != "3"
and(且) {<key>:<value>},{<key>:<value>} db.getCollection('company').find({"source":"5","last_login_at":{$gt:"2018-07-10 00:00:00"}}) where source = "3" or source = "5" and last_login_at > "2018-07-10 00:00:00"
or(或) {$or:[{<key>:<value>},{<key>:<value>}]} db.getCollection('company').find({$or:[{"source":"3"},{"source":"5"}]}) where source = "3" or source = "5"
and 聯合 or {<key>:<value>,$or:[{<key>:<value>},{<key>:<value>}]} db.getCollection('company').find({"source":"5",$or:[{"last_login_at":{$gt:"2018-07-10 00:00:00"}},{"last_login_at":{$lt:"2018-06-20 00:00:00"}}]}) where source = 5 and (last_login_at > "2018-07-10 00:00:00" or last_login_at < "2018-06-20 00:00:00")

4.2、過濾導出的數據-示例

基礎命令

mongoexport -h xxx -u xxxx -p xxxx --authenticationDatabase=admin --authenticationMechanism=SCRAM-SHA-1 --port 27017 -d account_service -c company --type=csv -f _id,name,mobile,show_count,last_login_at,active -o ./company.csv

以下命令,都是基於該命令的,即,將下列命令添加到基礎命令中,便組成了完整的條件過濾導出命令。

導出 source 等於 3 的所有數據

-q "{'source':'3'}"

導出 last_login_at 大於 2018-07-10 17:08:05 的所有數據

-q "{'last_login_at':{$lt:'2018-07-10 17:08:05'}}"

導出 last_login_at 小於 2018-07-10 17:08:05 的所有數據

-q "{'last_login_at':{$gt:'2018-07-10 17:08:05'}}"

導出 last_login_at 小於或等於 2018-07-10 17:08:05 的所有數據

-q "{'last_login_at':{$lte:'2018-07-10 17:08:05'}}"

導出 last_login_at 大於或等於 2018-07-10 17:08:05 的所有數據

-q "{'last_login_at':{$gte:'2018-07-10 17:08:05'}}"

導出 source 不等於 3 的所有數據

-q "{'source':{$ne:'3'}}"

導出 source 等於 5 且 last_login_at 大於 2018-07-10 00:00:00 的所有數據

-q "{'source':'5','last_login_at':{$gt:'2018-07-10 00:00:00'}}"

導出 source 等於 3 或 source 等於 5 的所有數據

-q "{$or:[{'source':'3'},{'source':'5'}]}"

導出 source 等於 5 並且 last_login_at 大於 2018-07-10 00:00:00 或 last_login_at 小於 2018-06-20 00:00:00 的所有數據

-q "{'source':'5',$or:[{'last_login_at':{$gt:'2018-07-10 00:00:00'}},{'last_login_at':{$lt:'2018-06-20 00:00:00'}}]}"
展開閱讀全文
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章