MongoDB基于时间戳的导数操作

发布时间:2020-06-22 01:42:34 作者:yangjunfeng
来源:网络 阅读:1017

上班有个需求,协助提取某数据系统中cashloanDb

涉及MongoDB集合: cardBill(时间段 6.1-8.28) , cardReport (时间段 2.1-8.28)
如果不支持根据时间提取,可提取全量数据。
使用mongo客户端命令进入数据库:
jsfkrs0:PRIMARY> show dbs;
2018-09-04T17:35:57.692+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>shellHelper.show@src/mongo/shell/utils.js:769:19
br/>@(shellhelp2):1:1
jsfkrs0:PRIMARY> use admin
switched to db admin
jsfkrs0:PRIMARY> db.auth('mangoadmin','password')
1
jsfkrs0:PRIMARY> show dbs;
admin 0.000GB
cashloanDb 96.287GB
fuf_mgdb 0.765GB
local 1.280GB

切换至我们要操作的db中
jsfkrs0:PRIMARY> use cashloanDb
switched to db cashloanDb

看一下这两个集合的全量数据有多少
jsfkrs0:PRIMARY> db.cardBill.count();
247126
jsfkrs0:PRIMARY> db.cardReport.count();
249128

先看一下这个表里面有没有时间戳我们取一条数据来看,有的话,我们可以基于这个时间戳来进行导出工作;
jsfkrs0:PRIMARY> db.cardBill.findOne();
{
"_id" : ObjectId("59e180110cxxxxxx"),
"timestamp" : NumberLong("1507950609625"),
"taskId" : "xxxxxx-b08d-11e7-bf17-xxxxx",
"bill" : "[{\"balance\":47000.00,\"deposits\":[],\"bills\":[{\"installments\":[],\"bill_id\":\"d81f1e60-a02d-11e7-a63a-00163e0dfac7\",\"bill_type\":\"DONE\",\"bank_name\":\"中信银行\",\"bill_month\":\"2017-04\",\"bill_date\":\"2017-04-14\",\"payment_due

这个时间戳有13位,精确到了毫秒,我们在导出的时候需要换算成Unix时间戳,并且也精确到毫秒才可以。
http://tool.chinaz.com/Tools/unixtime.aspx
MongoDB基于时间戳的导数操作

正常换算出来的只到秒位,只有10位。可以直接在换算出的Unix时间戳后面加000,或者换算的时候直接精确到毫秒,下来我们就可以用MangoDB自带的导出工具进行导出;
mongoexport -d cashloanDb -c cardBill-uadmin -ppassword --authenticationDatabase "admin" -q '{"timestamp":{$gte:NumberLong("1517414400000"),$lte:NumberLong("1535472000000")}}' -o cardBill.json

推荐阅读:
  1. MongoDB操作
  2. Java操作MongoDB

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

mongoexport mongodb

上一篇:优秀的程序员都热爱写作

下一篇:独立模式的Hadoop环境搭建

相关阅读

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

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