概述
十多年的蘭州網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整蘭州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“蘭州網(wǎng)站設計”,“蘭州網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
mongoexport命令行用于數(shù)據(jù)的導出,默認導出的文件格式為JSON格式。當然也可以指定特定的文件格式。
語法
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
說明:
引言
今天在用mongoexport導出滿足一定條件下的數(shù)據(jù)時,遇到了一個報錯,現(xiàn)紀錄下來,并且針對此錯誤對MongoDB 的 數(shù)字類型做了進一步的學習。
背景 及 報錯信息
今天接到一個業(yè)務需求,需要從MongoDB 數(shù)據(jù)庫 order集合中導出符合以下條件的數(shù)據(jù):
db.qqwj_order.find({"Source":NumberInt("21"),"Batch":"支付中的訂單提醒:2018/9/5","MsgContent":/還未完成在線付款/})
通過MongoDB 客戶端工具 【NOSQLBooster for MongoDB】查詢檢查,語句執(zhí)行正常,顯示相應記錄數(shù)為 15265。
導出數(shù)據(jù)使用mongoexport命令,執(zhí)行命令如下:
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號 -p '密碼' --authenticationDatabase 認證數(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í)行報錯:
XXX is not valid JSON: json: cannot unmarshal string into Go value of type json.NumberInt
錯誤截圖如下:
錯誤推斷及測試
因為報錯信息中NumberInt 關鍵字,此時去看我們的查詢條件正好也有此關鍵字,所以推測 是不是這個問題。
結(jié)果將導出命令中的 NumberInt("21")
直接替換為 21 ,再次執(zhí)行。
執(zhí)行命令為 :
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號 -p '密碼' --authenticationDatabase 認證數(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é)果為
結(jié)果表明修改后,數(shù)據(jù)成功導出。
錯誤解析與原理探究
為什么通過查詢器查看,數(shù)據(jù)就是 "Source" : NumberInt("21")
,但是在shell 中的執(zhí)行導出命令寫成"Source" : NumberInt("21")
就會報錯。而一定要轉(zhuǎn)換為"Source":21
查詢器查詢出的Source字段顯示:
明明就是"Source" : NumberInt("21")
,為什么復制到shell,執(zhí)行報錯???
回頭看,找原理。我們知道目前MongoDB 支持4中數(shù)據(jù)類型。
在MongoDB客戶端可以執(zhí)行查詢,但是在shell中無法執(zhí)行導出,那么會不會和這兩種工具有關?會不會和插入的NumberInt(數(shù)字) 還是NumberInt('數(shù)字‘)有關?
下面對假設進行驗證測試。
通過 NoSQLBooster for MongoDB 方式 插入測試數(shù)據(jù)
通過 shell方式插入測試數(shù)據(jù)
通過$type 去查看插入的數(shù)據(jù)類型
1》執(zhí)行db.numbers.find({n:{$type:1}}) // Type
為 Double;查詢Type 為 Double的數(shù)據(jù)
以上查詢結(jié)果顯示,不管是通過客戶端還是shell,當數(shù)字不指明數(shù)據(jù)類型時,插入的數(shù)字數(shù)據(jù)默認都是Double。
2》執(zhí)行命令 db.numbers.find({n:{$type:16}}) // Type
為 32-bit integer ;查詢Type 為 32-bit integer的數(shù)據(jù)
以上查詢表名,不管通過客戶端還是shell,指定的NumberInt(5)
還是NumberInt('5‘)
后臺都轉(zhuǎn)成統(tǒng)一32-bit integer 類型存儲了。
3》執(zhí)行命令 db.numbers.find({n:{$type:18}}) // Type
為 64-bit integer 查詢Type 為 64-bit integer的數(shù)據(jù)
以上查詢表名,不管通過客戶端還是shell,指定的NumberLong(5)
還是NumberLong('5')
后臺都轉(zhuǎn)成統(tǒng)一64-bit integer 類型存儲了。
以上的測試說明,當我們在存儲數(shù)字數(shù)據(jù)時會自動轉(zhuǎn)儲(不管什么客戶端工具,是shell還是 【NoSQLBooster for MongoDB】,不管 NumberLong(5) 還是NumberLong('5');NumberInt(5) 還是NumberInt('5‘))。
有點糊涂了吧? 如此這樣,那為什么 在查詢是報錯呢?
回頭再看錯誤提示:XXX is not valid JSON: json: cannot unmarshal string into Go value of type json.NumberInt。
其意思是shell 認為我們把一個字符類型的數(shù)據(jù)傳給了 json.NumberInt
。
那我如果將導出命令中的 NumberInt("21")
將 換成 NumberInt(21)
執(zhí)行命令為 :
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號 -p '密碼' --authenticationDatabase 認證數(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í)行也成功。
結(jié)論
說了很多總結(jié)下:
執(zhí)行失敗的導出命令是:
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號 -p '密碼' --authenticationDatabase 認證數(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í)行成功的導出命令是:
/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 數(shù)據(jù)庫 -u 賬號 -p '密碼' --authenticationDatabase 認證數(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 賬號 -p '密碼' --authenticationDatabase 認證數(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
三個導出命令不同的地方已用紅色字體標注。
P.S 1 :后來作者深究了一下,為什么同樣的查詢,通樣的查詢結(jié)果,有的顯示 "n" : 5 ; 有的顯示 "n" : NumberInt("5")。嘻嘻 》》》》版本不同而已。
舊版本(部分)的顯示
新版本(例如nosqlbooster4mongo-4.7.1)的顯示
P.S 2 :在存儲數(shù)字數(shù)據(jù)時,到底會存儲為何種數(shù)據(jù)類型,其實和語言的的驅(qū)動有關。例如在Ruby 和 Python 語言里在序列化整數(shù)時,驅(qū)動會自動確定是否編碼為32-bit integer 還是64-bit integer;shell 需要顯示指定才可以。+
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
當前文章:MongoDB執(zhí)行mongoexport時的異常及分析(數(shù)字類型的查詢)
標題URL:http://aaarwkj.com/article12/ihppgc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、App開發(fā)、全網(wǎng)營銷推廣、電子商務、企業(yè)網(wǎng)站制作、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)