温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

发布时间:2020-10-24 12:57:13 来源:脚本之家 阅读:209 作者:东山絮柳仔 栏目:MongoDB数据库

概述

mongoexport命令行用于数据的导出,默认导出的文件格式为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

说明:

  • -h:数据库宿主机的IP
  • -u:数据库用户名
  • -p:数据库密码
  • -d:数据库名字
  • -c:集合的名字
  • -f:导出的列名
  • -q:导出数据的过滤条件
  • --csv:导出格式为csv 

引言

今天在用mongoexport导出满足一定条件下的数据时,遇到了一个报错,现纪录下来,并且针对此错误对MongoDB 的 数字类型做了进一步的学习。

背景 及 报错信息

今天接到一个业务需求,需要从MongoDB 数据库 order集合中导出符合以下条件的数据:

db.qqwj_order.find({"Source":NumberInt("21"),"Batch":"支付中的订单提醒:2018/9/5","MsgContent":/还未完成在线付款/})

通过MongoDB 客户端工具 【NoSQLBooster for MongoDB】查询检查,语句执行正常,显示相应记录数为 15265。

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

导出数据使用mongoexport命令,执行命令如下:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{ "Source":NumberInt("21"),"Batch":"支付中的订单提醒:2018/9/5","MsgContent":/还未完成在线付款/}' -o /data/mongodb_back/sms.csv

但是执行报错:

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

错误截图如下:

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

错误推断及测试

因为报错信息中NumberInt 关键字,此时去看我们的查询条件正好也有此关键字,所以推测 是不是这个问题。

结果将导出命令中的 NumberInt("21") 直接替换为 21 ,再次执行。

执行命令为 :

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{"Source":21,"Batch":"支付中的订单提醒:2018/9/5","MsgContent":/还未完成在线付款/}' -o /data/mongodb_back/sms.csv

执行结果为

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

结果表明修改后,数据成功导出。

错误解析与原理探究

为什么通过查询器查看,数据就是 "Source" : NumberInt("21") ,但是在shell 中的执行导出命令写成"Source" : NumberInt("21") 就会报错。而一定要转换为"Source":21

查询器查询出的Source字段显示:

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

明明就是"Source" : NumberInt("21") ,为什么复制到shell,执行报错???

回头看,找原理。我们知道目前MongoDB 支持4中数据类型。

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

在MongoDB客户端可以执行查询,但是在shell中无法执行导出,那么会不会和这两种工具有关?会不会和插入的NumberInt(数字) 还是NumberInt('数字‘)有关?

下面对假设进行验证测试。

通过 NoSQLBooster for MongoDB 方式 插入测试数据

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

通过 shell方式插入测试数据

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

通过$type 去查看插入的数据类型

1》执行db.numbers.find({n:{$type:1}}) // Type 为 Double;查询Type 为 Double的数据

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

以上查询结果显示,不管是通过客户端还是shell,当数字不指明数据类型时,插入的数字数据默认都是Double。

2》执行命令 db.numbers.find({n:{$type:16}}) // Type 为 32-bit integer ;查询Type 为 32-bit integer的数据

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

以上查询表名,不管通过客户端还是shell,指定的NumberInt(5) 还是NumberInt('5‘) 后台都转成统一32-bit integer 类型存储了。

3》执行命令 db.numbers.find({n:{$type:18}}) // Type 为 64-bit integer 查询Type 为 64-bit integer的数据

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

以上查询表名,不管通过客户端还是shell,指定的NumberLong(5) 还是NumberLong('5') 后台都转成统一64-bit integer 类型存储了。

以上的测试说明,当我们在存储数字数据时会自动转储(不管什么客户端工具,是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 认为我们把一个字符类型的数据传给了 json.NumberInt

那我如果将导出命令中的 NumberInt("21") 将 换成 NumberInt(21)

执行命令为 :

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --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执行mongoexport时的异常及分析(数字类型的查询)

执行也成功。

结论

说了很多总结下:

执行失败的导出命令是:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q '{ "Source":NumberInt("21"),"Batch":"支付中的订单提醒:2018/9/5","MsgContent":/还未完成在线付款/}' -o /data/mongodb_back/sms.csv

执行成功的导出命令是:

/data/mongodb/mongobin344/bin/mongoexport -h 172.X.X.XXX --port 端口 --db 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --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 数据库 -u 账号 -p '密码' --authenticationDatabase 认证数据库 --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 :后来作者深究了一下,为什么同样的查询,通样的查询结果,有的显示 "n" : 5 ; 有的显示 "n" : NumberInt("5")。嘻嘻 》》》》版本不同而已。

旧版本(部分)的显示

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

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

MongoDB执行mongoexport时的异常及分析(数字类型的查询)

P.S 2 :在存储数字数据时,到底会存储为何种数据类型,其实和语言的的驱动有关。例如在Ruby 和 Python 语言里在序列化整数时,驱动会自动确定是否编码为32-bit integer 还是64-bit integer;shell 需要显示指定才可以。+

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI