1

我对 MongoDb 非常陌生,我正在尝试是否可以使用它来存储时间序列数据。

我插入了以下数据...

{
"_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"
    }, 
  ....

因此,每个文档都是以 1 秒为间隔保存 DateTimes 的数组 Readings。

因此,如果我想在 2 个日期时间之间进行查询,我正在Robomongo查询窗口中尝试以下操作...

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它应该实际返回它们的位置。

我的语法一定不正确,但我只是找不到它有什么问题以及如何在嵌套数组中搜索日期时间,就像我在这里一样。有人有什么想法吗?

提前致谢!

4

1 回答 1

1
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')]}
                   ]
               }
            }
         }
      }
   }
])
于 2016-07-13T08:57:19.103 回答