问题描述
我是MongoDb的新手,正在尝试看看是否可以使用它来存储时间序列数据.
I am very new to MongoDb and experimenting to see if I can use it to store time series data.
我插入了以下数据...
I have inserted the following data...
{
"_id" : ObjectId("5785f186ed936527c05efa10"),
"Timestamp" : ISODate("2006-07-13T07:42:00.000Z"),
"Label1" : "Lab1",
"Attr" : "atrr1",
"Readings" : [
{
"DateTime" : ISODate("2006-07-13T07:42:06.355Z"),
"Value" : "22"
},
{
"DateTime" : ISODate("2006-07-13T07:42:07.355Z"),
"Value" : "22"
},
{
"DateTime" : ISODate("2006-07-13T07:42:08.355Z"),
"Value" : "22"
},
....
因此,每个文档都是数组Readings,它们以1秒的间隔保存DateTimes.
So, each document as the array Readings that hold DateTimes in 1 second intervals.
因此,如果要在2个日期时间之间进行查询,请在 Robomongo 查询窗口中尝试以下操作.
So, if I want to query between 2 datetimes, I am trying the following in the Robomongo query window...
db.getCollection('Timedata').find(
{
'Readings.DateTime':
{ $gt: '2005-07-13 07:42:13.355Z', $lt: '2010-07-13 07:42:13.355Z'}
})
但是,它总是返回Fetched 0 record(s) in 11ms
,而实际上应该全部返回.
However this always returns Fetched 0 record(s) in 11ms
where as it should actually return them all.
我的语法必须不正确,但是我只是无法找到它的问题以及如何在嵌套数组中搜索日期时间.会有人有什么想法吗?
My syntax must be incorrect, but I just cannot find what is wrong with it and how to search for datetimes in a nested array as I have here. Would anyone have any ideas?
提前谢谢!
推荐答案
db.getCollection('Timedata').find(
{
Readings: {
$elemMatch:
{DateTime:
{
$gt: ISODate('2005-07-13 07:42:13.355Z'),
$lt: ISODate('2010-07-13 07:42:13.355Z')
}
}
}
});
db.getCollection('Timedata').aggregate([
{
$match:{
Readings: {
$elemMatch:
{DateTime:
{
$gt: ISODate('2005-07-13 07:42:13.355Z'),
$lt: ISODate('2010-07-13 07:42:13.355Z')
}
}
}
}
},
{
$project: {
_id:1,
Timestamp:1,
Label1:1,
Attr:1,
Readings: {
$filter: {
input: "$Readings",
as: "item",
cond: {
$and: [
{$gt: [ "$$item.DateTime", ISODate('2006-07-13 07:00:13.355Z') ]},
{$lt: ["$$item.DateTime", ISODate('2010-07-15 07:42:13.355Z')]}
]
}
}
}
}
}
])
这篇关于MongoDb使用Robomongo在dateTimes之间搜索嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!