使用ObjectID字段查找15分钟数据 [英] Find 15 mins data with ObjectID field

查看:98
本文介绍了使用ObjectID字段查找15分钟数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ObjectID 查找最近15分钟的数据.

I am finding latest 15 minutes data with ObjectID.

查找查询:

db.data.find({ 
    _id: { 
        $gte: new ObjectId(
            Math.floor(new Date(ISODate().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000"
        ) 
    } 
})

输出:

{ "_id" : ObjectId("57c3ef6837ff7057a2ad3cca"), "User" : "Karthick", age: 26 }

{ "_id" : ObjectId("57c3ef6837ff7057a2ad3cce"), "User" : "Raja", age: 29 }

但是,我正在尝试进行数据转储,

But, I am trying to take the data dump,

mongodump --query '{ _id: { $gte: new ObjectId(Math.floor(new Date(ISODate().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000") } }' --db test --collection data --username abcdef --password abc@123 --authenticationDatabase admin -o "mongodump"

错误

Failed: error parsing query as json: invalid character 't' in literal MaxKey (expecting 'x')   

我收到上述错误.如何解决此问题,或者如何将15分钟的数据转储放入cronjob?

I am getting the above error. How do I solve this issue or how do I put 15 minutes data dump in cronjob?

推荐答案

问题是您的查询不是有效的JSON,因为它包含要评估的JS表达式(您的计算带有日期).
因此,您基本上必须制作一个脚本或执行一些shell杂技,以在将查询JSON传递给mongoexport

The problem is your query is not valid JSON as it contains JS expressions to be evaluated (your calculations with the date).
So you basically have to make a script or do some shell acrobatics which generates the query JSON prior passing it to mongoexport

您可能会发现链接非常有用,它确实可以使用python

You may find this link useful which does exactly that with python

此外,我做了(快速处理)与节点类似的操作,即使用该内容创建了一个文件 query.js ,该文件实际上创建了您的查询JSON并将其写入控制台

In addition I did (quick & dirty) something similar with node, i.e. created a file query.js with this content which essentially creates your query JSON and writes it to the console

var oid = Math.floor(new Date(new Date().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000";
console.log('{ "_id": { "$gte": new ObjectId("' + oid + '") } }');

以便您现在可以像这样

mongoexport ... --query "$(node query.js)" ...

希望有帮助

这篇关于使用ObjectID字段查找15分钟数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆