使用 Mongodb 查询以获取最后 X 分钟的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18233945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Query to get last X minutes data with Mongodb
提问by Goku
I'm beginning with mongodb, and I'm trying to query my db that have this document format:
我从 mongodb 开始,我正在尝试查询具有以下文档格式的数据库:
{ "_id" : ObjectId("520b8b3f8bd94741bf006033"), "value" : 0.664, "timestamp" : ISODate("2013-08-14T13:48:35Z"), "cr" : ISODate("2013-08-14T13:50:55.834Z") }
I can get the last records from a datetime with this query:
我可以使用此查询从日期时间获取最后一条记录:
> db.mycol.find({timestamp:{$gt: ISODate("2013-08-14T13:48:00Z")}}).sort({x:1});
But I'm trying to get a set with the value fields and timestamps from 18 minutes ago, how can I build a query for this? Thanks everybody for your patience...
但是我试图从 18 分钟前获取一个包含值字段和时间戳的集合,我该如何为此构建查询?谢谢大家的耐心...
回答by H.D.
For the 18 minutes part, that's not really about MongoDB, but about JavaScript and what's available in the mongo shell:
在 18 分钟的部分中,这并不是真正关于 MongoDB,而是关于 JavaScript 和 mongo shell 中可用的内容:
query = {
timestamp: { // 18 minutes ago (from now)
$gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
}
}
Works in the mongo shell, but using Mongo drivers for other languages would be really different.
在 mongo shell 中工作,但使用其他语言的 Mongo 驱动程序将非常不同。
To "project" over a smaller schema with both values and timestamps:
要“投影”具有值和时间戳的较小架构:
projection = {
_id: 0,
value: 1,
timestamp: 1,
}
Applying both:
两者同时应用:
db.mycol.find(query, projection).sort({timestamp: 1});
Well, that's still not a "set" since there might be duplicates. To get rid of them you can use the $group
from the aggregation framework:
好吧,这仍然不是“集合”,因为可能存在重复项。要摆脱它们,您可以使用$group
来自聚合框架的:
db.mycol.aggregate([
{$match: query},
{$group: {
_id: {
value: "$value",
timestamp: "$timestamp",
}
}},
{$project: {
value: "$_id.value",
timestamp: "$_id.timestamp",
}},
{$sort: {timestamp: 1}},
])
回答by Goku
Wow, thanks to @Alistair_Nelson I was able to get the data from n minutes ago, for example to get the last 18 minutes from ISODate("2013-08-14T14:00:00Z"):
哇,感谢@Alistair_Nelson,我能够从 n 分钟前获取数据,例如从 ISODate("2013-08-14T14:00:00Z") 获取最后 18 分钟:
db.mycol.find({timestamp:{$gt: new Date(ISODate("2013-08-14T14:00:00Z")-18*60000)}})
To get only the fields I need:
只获取我需要的字段:
db.mycol.find({timestamp:{$gt: new Date(ISODate("2013-08-14T14:00:00Z")-18*60000)}},{value:1,timestamp:1, _id:0})
回答by Pravin
You could also do below
你也可以在下面做
db.getCollection('collectionName').find({timestamp : {$gte: new Date().getTime()-(60*60*1000) } } )
The above query ll give you records of timestamp b/w now and 60 mins. if you like more then 60 mins - say 2 hrs you could change expression to (2*60*60*1000) for 30 mins (30*60*1000)
上面的查询将为您提供时间戳 b/w 现在和 60 分钟的记录。如果您喜欢超过 60 分钟 - 比如说 2 小时,您可以将表达式更改为 (2*60*60*1000) 30 分钟 (30*60*1000)
回答by Mukesh Kumar
you can access the data of current timestamp from mongodb using nodejs
您可以使用 nodejs 从 mongodb 访问当前时间戳的数据
const collection1 = dbo.collection('customers');
var dateq = new Date();
collection1.find({ "Timestamp" : { $gt: new Date(dateq.getTime() - 6000)}
}).toArray(function(err , docs){
console.log(docs);
}
code end
代码结束