mongoexport -help options: &nb..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

發(fā)布時(shí)間:2020-10-24 12:57:13 來源:腳本之家 閱讀:233 作者:東山絮柳仔 欄目:MongoDB數(shù)據(jù)庫

概述

mongoexport命令行用于數(shù)據(jù)的導(dǎo)出,默認(rèn)導(dǎo)出的文件格式為JSON格式。當(dāng)然也可以指定特定的文件格式。

語法

C:\mongo\bin>mongoexport -help
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more
                          verbosity e.g. -vvvvv)
  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)
  --port arg              server port. Can also use --host hostname:port
  --ipv6                  enable IPv6 support (disabled by default)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod database files in the given
                          path, instead of connecting to a mongod  server -
                          needs to lock the data directory, so cannot be used
                          if a mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate
                          directory
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  -f [ --fields ] arg     comma separated list of field names e.g. -f name,age
  --fieldFile arg         file with fields names - 1 per line
  -q [ --query ] arg      query filter, as a JSON string
  --csv                   export to csv instead of json
  -o [ --out ] arg        output file; if not specified, stdout is used
  --jsonArray             output to a json array rather than one object per
                          Line

說明:

  • -h:數(shù)據(jù)庫宿主機(jī)的IP
  • -u:數(shù)據(jù)庫用戶名
  • -p:數(shù)據(jù)庫密碼
  • -d:數(shù)據(jù)庫名字
  • -c:集合的名字
  • -f:導(dǎo)出的列名
  • -q:導(dǎo)出數(shù)據(jù)的過濾條件
  • --csv:導(dǎo)出格式為csv 

引言

今天在用mongoexport導(dǎo)出滿足一定條件下的數(shù)據(jù)時(shí),遇到了一個(gè)報(bào)錯(cuò),現(xiàn)紀(jì)錄下來,并且針對(duì)此錯(cuò)誤對(duì)MongoDB 的 數(shù)字類型做了進(jìn)一步的學(xué)習(xí)。

背景 及 報(bào)錯(cuò)信息

今天接到一個(gè)業(yè)務(wù)需求,需要從MongoDB 數(shù)據(jù)庫 order集合中導(dǎo)出符合以下條件的數(shù)據(jù):

db.qqwj_order.find({"Source":NumberInt("21"),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/})

通過MongoDB 客戶端工具 【NoSQLBooster for MongoDB】查詢檢查,語句執(zhí)行正常,顯示相應(yīng)記錄數(shù)為 15265。

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

導(dǎo)出數(shù)據(jù)使用mongoexport命令,執(zhí)行命令如下:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{ "Source":NumberInt("21"),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

但是執(zhí)行報(bào)錯(cuò):

XXX is not valid JSON: json: cannot unmarshal string into Go value of type json.NumberInt

錯(cuò)誤截圖如下:

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

錯(cuò)誤推斷及測試

因?yàn)閳?bào)錯(cuò)信息中NumberInt 關(guān)鍵字,此時(shí)去看我們的查詢條件正好也有此關(guān)鍵字,所以推測 是不是這個(gè)問題。

結(jié)果將導(dǎo)出命令中的 NumberInt("21") 直接替換為 21 ,再次執(zhí)行。

執(zhí)行命令為 :

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source":21,"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

執(zhí)行結(jié)果為

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

結(jié)果表明修改后,數(shù)據(jù)成功導(dǎo)出。

錯(cuò)誤解析與原理探究

為什么通過查詢器查看,數(shù)據(jù)就是 "Source" : NumberInt("21") ,但是在shell 中的執(zhí)行導(dǎo)出命令寫成"Source" : NumberInt("21") 就會(huì)報(bào)錯(cuò)。而一定要轉(zhuǎn)換為"Source":21

查詢器查詢出的Source字段顯示:

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

明明就是"Source" : NumberInt("21") ,為什么復(fù)制到shell,執(zhí)行報(bào)錯(cuò)???

回頭看,找原理。我們知道目前MongoDB 支持4中數(shù)據(jù)類型。

  • double
  • 32-bit integer
  • 64-bit integer
  • Decimal (New in version 3.4.)

在MongoDB客戶端可以執(zhí)行查詢,但是在shell中無法執(zhí)行導(dǎo)出,那么會(huì)不會(huì)和這兩種工具有關(guān)?會(huì)不會(huì)和插入的NumberInt(數(shù)字) 還是NumberInt('數(shù)字‘)有關(guān)?

下面對(duì)假設(shè)進(jìn)行驗(yàn)證測試。

通過 NoSQLBooster for MongoDB 方式 插入測試數(shù)據(jù)

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

通過 shell方式插入測試數(shù)據(jù)

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

通過$type 去查看插入的數(shù)據(jù)類型

1》執(zhí)行db.numbers.find({n:{$type:1}}) // Type 為 Double;查詢Type 為 Double的數(shù)據(jù)

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

以上查詢結(jié)果顯示,不管是通過客戶端還是shell,當(dāng)數(shù)字不指明數(shù)據(jù)類型時(shí),插入的數(shù)字?jǐn)?shù)據(jù)默認(rèn)都是Double。

2》執(zhí)行命令 db.numbers.find({n:{$type:16}}) // Type 為 32-bit integer ;查詢Type 為 32-bit integer的數(shù)據(jù)

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

以上查詢表名,不管通過客戶端還是shell,指定的NumberInt(5) 還是NumberInt('5‘) 后臺(tái)都轉(zhuǎn)成統(tǒng)一32-bit integer 類型存儲(chǔ)了。

3》執(zhí)行命令 db.numbers.find({n:{$type:18}}) // Type 為 64-bit integer 查詢Type 為 64-bit integer的數(shù)據(jù)

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

以上查詢表名,不管通過客戶端還是shell,指定的NumberLong(5) 還是NumberLong('5') 后臺(tái)都轉(zhuǎn)成統(tǒng)一64-bit integer 類型存儲(chǔ)了。

以上的測試說明,當(dāng)我們?cè)诖鎯?chǔ)數(shù)字?jǐn)?shù)據(jù)時(shí)會(huì)自動(dòng)轉(zhuǎn)儲(chǔ)(不管什么客戶端工具,是shell還是 【NoSQLBooster for MongoDB】,不管 NumberLong(5) 還是NumberLong('5');NumberInt(5) 還是NumberInt('5‘))。

有點(diǎn)糊涂了吧? 如此這樣,那為什么 在查詢是報(bào)錯(cuò)呢?

回頭再看錯(cuò)誤提示:XXX is not valid JSON: json: cannot unmarshal string into Go value of type json.NumberInt。

其意思是shell 認(rèn)為我們把一個(gè)字符類型的數(shù)據(jù)傳給了 json.NumberInt 。

那我如果將導(dǎo)出命令中的 NumberInt("21") 將 換成 NumberInt(21)

執(zhí)行命令為 :

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source": NumberInt(21),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

執(zhí)行也成功。

結(jié)論

說了很多總結(jié)下:

執(zhí)行失敗的導(dǎo)出命令是:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{ "Source":NumberInt("21"),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

執(zhí)行成功的導(dǎo)出命令是:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source":21,"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source": NumberInt(21),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv

三個(gè)導(dǎo)出命令不同的地方已用紅色字體標(biāo)注。

P.S 1 :后來作者深究了一下,為什么同樣的查詢,通樣的查詢結(jié)果,有的顯示 "n" : 5 ; 有的顯示 "n" : NumberInt("5")。嘻嘻 》》》》版本不同而已。

舊版本(部分)的顯示

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

新版本(例如nosqlbooster4mongo-4.7.1)的顯示

MongoDB執(zhí)行mongoexport時(shí)的異常及分析(數(shù)字類型的查詢)

P.S 2 :在存儲(chǔ)數(shù)字?jǐn)?shù)據(jù)時(shí),到底會(huì)存儲(chǔ)為何種數(shù)據(jù)類型,其實(shí)和語言的的驅(qū)動(dòng)有關(guān)。例如在Ruby 和 Python 語言里在序列化整數(shù)時(shí),驅(qū)動(dòng)會(huì)自動(dòng)確定是否編碼為32-bit integer 還是64-bit integer;shell 需要顯示指定才可以。+

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI