概述
mongoexport命令行用于數(shù)據(jù)的導(dǎo)出,默認(rèn)導(dǎo)出的文件格式為JSON格式。當(dāng)然也可以指定特定的文件格式。
語(yǔ)法
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
說(shuō)明:
- -h:數(shù)據(jù)庫(kù)宿主機(jī)的IP
- -u:數(shù)據(jù)庫(kù)用戶名
- -p:數(shù)據(jù)庫(kù)密碼
- -d:數(shù)據(jù)庫(kù)名字
- -c:集合的名字
- -f:導(dǎo)出的列名
- -q:導(dǎo)出數(shù)據(jù)的過(guò)濾條件
- --csv:導(dǎo)出格式為csv
引言
今天在用mongoexport導(dǎo)出滿足一定條件下的數(shù)據(jù)時(shí),遇到了一個(gè)報(bào)錯(cuò),現(xiàn)紀(jì)錄下來(lái),并且針對(duì)此錯(cuò)誤對(duì)MongoDB 的 數(shù)字類型做了進(jìn)一步的學(xué)習(xí)。
背景 及 報(bào)錯(cuò)信息
今天接到一個(gè)業(yè)務(wù)需求,需要從MongoDB 數(shù)據(jù)庫(kù) order集合中導(dǎo)出符合以下條件的數(shù)據(jù):
db.qqwj_order.find({"Source":NumberInt("21"),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/})
通過(guò)MongoDB 客戶端工具 【NoSQLBooster for MongoDB】查詢檢查,語(yǔ)句執(zhí)行正常,顯示相應(yīng)記錄數(shù)為 15265。
![](/d/20211018/2cc9d6d9920f68b48cd24625ebce4952.gif)
導(dǎo)出數(shù)據(jù)使用mongoexport命令,執(zhí)行命令如下:
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --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ò)誤截圖如下:
![](/d/20211018/bd74649d1372b6f4ab43b4ed390e34cd.gif)
錯(cuò)誤推斷及測(cè)試
因?yàn)閳?bào)錯(cuò)信息中NumberInt 關(guān)鍵字,此時(shí)去看我們的查詢條件正好也有此關(guān)鍵字,所以推測(cè) 是不是這個(gè)問(wèn)題。
結(jié)果將導(dǎo)出命令中的 NumberInt("21")
直接替換為 21 ,再次執(zhí)行。
執(zhí)行命令為 :
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --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é)果為
![](/d/20211018/79aef5fa52b8e763bada8ca876a7ca25.gif)
結(jié)果表明修改后,數(shù)據(jù)成功導(dǎo)出。
錯(cuò)誤解析與原理探究
為什么通過(guò)查詢器查看,數(shù)據(jù)就是 "Source" : NumberInt("21")
,但是在shell 中的執(zhí)行導(dǎo)出命令寫成"Source" : NumberInt("21")
就會(huì)報(bào)錯(cuò)。而一定要轉(zhuǎn)換為"Source":21
查詢器查詢出的Source字段顯示:
![](/d/20211018/604d08053a1cc1032df1f209bf7c0fd9.gif)
明明就是"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中無(wú)法執(zhí)行導(dǎo)出,那么會(huì)不會(huì)和這兩種工具有關(guān)?會(huì)不會(huì)和插入的NumberInt(數(shù)字) 還是NumberInt('數(shù)字‘)有關(guān)?
下面對(duì)假設(shè)進(jìn)行驗(yàn)證測(cè)試。
通過(guò) NoSQLBooster for MongoDB 方式 插入測(cè)試數(shù)據(jù)
![](/d/20211018/22427f5d2012af1a721a33be9f01cb76.gif)
通過(guò) shell方式插入測(cè)試數(shù)據(jù)
![](/d/20211018/594088b9fa16a601bb725b3d4c61c0bb.gif)
通過(guò)$type 去查看插入的數(shù)據(jù)類型
1》執(zhí)行db.numbers.find({n:{$type:1}}) // Type
為 Double;查詢Type 為 Double的數(shù)據(jù)
![](/d/20211018/c1a433d2ba1f02a0a56a902b0cde9b85.gif)
以上查詢結(jié)果顯示,不管是通過(guò)客戶端還是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ù)
![](/d/20211018/41cd39fec3f2f38ac1745b92bfa34c21.gif)
以上查詢表名,不管通過(guò)客戶端還是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ù)
![](/d/20211018/5de5f6252b666d4d779eadf13c6d3938.gif)
以上查詢表名,不管通過(guò)客戶端還是shell,指定的NumberLong(5)
還是NumberLong('5')
后臺(tái)都轉(zhuǎn)成統(tǒng)一64-bit integer 類型存儲(chǔ)了。
以上的測(cè)試說(shuō)明,當(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ù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source": NumberInt(21),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/}' -o /data/mongodb_back/sms.csv
![](/d/20211018/13d335d0745caabb0edbb50bdbaa139d.gif)
執(zhí)行也成功。
結(jié)論
說(shuō)了很多總結(jié)下:
執(zhí)行失敗的導(dǎo)出命令是:
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --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ù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --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ù)庫(kù) -u 賬號(hào) -p '密碼' --authenticationDatabase 認(rèn)證數(shù)據(jù)庫(kù) --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 :后來(lái)作者深究了一下,為什么同樣的查詢,通樣的查詢結(jié)果,有的顯示 "n" : 5 ; 有的顯示 "n" : NumberInt("5")。嘻嘻 》》》》版本不同而已。
舊版本(部分)的顯示
![](/d/20211018/529852a7e5dc1638ecad9782fceca140.gif)
新版本(例如nosqlbooster4mongo-4.7.1)的顯示
![](/d/20211018/b0a78f31c834afaa27fe97ed2ebd84cf.gif)
P.S 2 :在存儲(chǔ)數(shù)字?jǐn)?shù)據(jù)時(shí),到底會(huì)存儲(chǔ)為何種數(shù)據(jù)類型,其實(shí)和語(yǔ)言的的驅(qū)動(dòng)有關(guān)。例如在Ruby 和 Python 語(yǔ)言里在序列化整數(shù)時(shí),驅(qū)動(dòng)會(huì)自動(dòng)確定是否編碼為32-bit integer 還是64-bit integer;shell 需要顯示指定才可以。+
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- MongoDB使用mongoexport和mongoimport命令,批量導(dǎo)出和導(dǎo)入JSON數(shù)據(jù)到同一張表的實(shí)例
- MongoDB mongoexport工具的使用簡(jiǎn)介