在mongodb中查询多级子文档

时间:2019-07-08 18:04:13

标签: javascript mongodb mongoose

我正在尝试在mongoDB中对时间序列数据进行多级升级。在博客文章中,他们使用$ push添加到子文档中,但是我将如何检查多个子文档以查看值是否存在?此外,是否有可能在每个级别上进行增补?如果不存在,则创建日期/小时/分钟,如果不存在,则创建小时/分钟,等等。

// Structure to query and upsert
{
  date: ISODate("2019-07-08"),
  avgDailyValA: 35,
  hours: [
    { 
      hour: 1,
      avgHourlyValA: 33, 
      minutes: [
        {
          minute: 1,
          avgMinuteValA: 32
        }
      ]
    }
  ]
}

sample = {val: 59, time: 1535530450}
day = ISODate("2018-08-29")
db.iot.updateOne({deviceid: 1234, sensorid: 3, nsamples: {$lt: 200}, day: day},
             {$push: {samples: sample},
              $min: {first: sample.time},
              $max: {last: sample.time},
              $inc: {nsamples: 1}, {upsert: true} )

MongoDB Blog Recommendation 3rd party Blog Recommendation

0 个答案:

没有答案