Mongoose以最小相对时差获得最后X值

时间:2017-07-04 15:58:06

标签: mongodb mongoose

我正在尝试获取确切数量的数据库条目,这些数据库条目彼此之间的时间差最小。例如。传感器写入数据库约。每1000毫秒,但我想阅读,例如具有最小时间差异的20个值。 1小时。

目前,我正在阅读$ gt,但这会从数据库中提取72000(20x60mx60s)条目。

然后我将过滤:

  filterSensorHistory: (data, callback)->
    self = @
    data = _.sortBy(data, 'timestamp').reverse()
    lastEntry = moment(data[0].timestamp).add(5, self.timeUnit) #add 5 units to the last entry (for processing delay), works seconds & minutes
    data = data.filter (dataItem, index)->
      return false if lastEntry.diff(moment(dataItem.timestamp), self.timeUnit) <= 1 #if less than 1 time unit diff. reject
      lastEntry = moment(dataItem.timestamp)
      return true
    data = data.splice(0,HISTORY_LENGTH)
    data = data.map (dataItem)->
      return { x: moment(dataItem.timestamp).toDate(), y: dataItem.value }
    data.reverse()
    return callback null, data

我想知道我是否可以在数据库级别上提高效率?

Mongo版本:2.4.10

编辑:示例数据

我想查询每秒,分钟或小时。例如。每分钟:1, 12, 22, 32, 44 ...。在这种情况下,我基本上可以.skip(12),但传感器有时也可能会关闭。

/* 1 */
{
    "value" : 54.1168212890625,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0853a7d7be048274809e"),
    "timestamp" : ISODate("2017-07-04T21:27:47.290Z"),
    "__v" : 0
}

/* 2 */
{
    "value" : 54.4702064708767,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0853a7d7be048274809d"),
    "timestamp" : ISODate("2017-07-04T21:27:47.078Z"),
    "__v" : 0
}

/* 3 */
{
    "value" : 54.4899286008879,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c084ba7d7be0482748097"),
    "timestamp" : ISODate("2017-07-04T21:27:39.745Z"),
    "__v" : 0
}

/* 4 */
{
    "value" : 54.5661574715879,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0844a7d7be0482748095"),
    "timestamp" : ISODate("2017-07-04T21:27:32.731Z"),
    "__v" : 0
}

/* 5 */
{
    "value" : 54.683723765302,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c083ea7d7be0482748093"),
    "timestamp" : ISODate("2017-07-04T21:27:26.213Z"),
    "__v" : 0
}

/* 6 */
{
    "value" : 54.7179478596223,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0837a7d7be0482748091"),
    "timestamp" : ISODate("2017-07-04T21:27:19.703Z"),
    "__v" : 0
}

/* 7 */
{
    "value" : 54.7588104883133,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0831a7d7be048274808f"),
    "timestamp" : ISODate("2017-07-04T21:27:13.171Z"),
    "__v" : 0
}

/* 8 */
{
    "value" : 54.5089721679688,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c082aa7d7be048274808d"),
    "timestamp" : ISODate("2017-07-04T21:27:06.898Z"),
    "__v" : 0
}

/* 9 */
{
    "value" : 54.8077643741843,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c082aa7d7be048274808c"),
    "timestamp" : ISODate("2017-07-04T21:27:06.634Z"),
    "__v" : 0
}

/* 10 */
{
    "value" : 54.825757605678,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0822a7d7be0482748086"),
    "timestamp" : ISODate("2017-07-04T21:26:58.975Z"),
    "__v" : 0
}

/* 11 */
{
    "value" : 54.8885850799935,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c081ca7d7be0482748084"),
    "timestamp" : ISODate("2017-07-04T21:26:52.003Z"),
    "__v" : 0
}

/* 12 */
{
    "value" : 54.9645862093469,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0815a7d7be0482748082"),
    "timestamp" : ISODate("2017-07-04T21:26:45.479Z"),
    "__v" : 0
}

/* 13 */
{
    "value" : 55.0455007725319,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c080fa7d7be0482748080"),
    "timestamp" : ISODate("2017-07-04T21:26:38.970Z"),
    "__v" : 0
}

/* 14 */
{
    "value" : 55.1649881938774,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0808a7d7be048274807e"),
    "timestamp" : ISODate("2017-07-04T21:26:32.423Z"),
    "__v" : 0
}

/* 15 */
{
    "value" : 55.2170605872354,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0801a7d7be048274807c"),
    "timestamp" : ISODate("2017-07-04T21:26:25.918Z"),
    "__v" : 0
}

/* 16 */
{
    "value" : 55.2759415120643,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07fba7d7be048274807a"),
    "timestamp" : ISODate("2017-07-04T21:26:19.400Z"),
    "__v" : 0
}

/* 17 */
{
    "value" : 55.3524623713558,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07f4a7d7be0482748078"),
    "timestamp" : ISODate("2017-07-04T21:26:12.890Z"),
    "__v" : 0
}

/* 18 */
{
    "value" : 55.470471603603,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07eea7d7be0482748076"),
    "timestamp" : ISODate("2017-07-04T21:26:06.363Z"),
    "__v" : 0
}

/* 19 */
{
    "value" : 55.5055806131762,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07e7a7d7be0482748074"),
    "timestamp" : ISODate("2017-07-04T21:25:59.807Z"),
    "__v" : 0
}

/* 20 */
{
    "value" : 55.5441593764087,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07e1a7d7be0482748072"),
    "timestamp" : ISODate("2017-07-04T21:25:53.306Z"),
    "__v" : 0
}

/* 21 */
{
    "value" : 55.5964646516023,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07daa7d7be0482748070"),
    "timestamp" : ISODate("2017-07-04T21:25:46.770Z"),
    "__v" : 0
}

/* 22 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07d4a7d7be048274806e"),
    "timestamp" : ISODate("2017-07-04T21:25:40.295Z"),
    "__v" : 0
}

/* 23 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07d4a7d7be048274806d"),
    "timestamp" : ISODate("2017-07-04T21:25:40.094Z"),
    "__v" : 0
}

/* 24 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07cba7d7be0482748066"),
    "timestamp" : ISODate("2017-07-04T21:25:31.653Z"),
    "__v" : 0
}

/* 25 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07c4a7d7be0482748063"),
    "timestamp" : ISODate("2017-07-04T21:25:24.774Z"),
    "__v" : 0
}

/* 26 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07bea7d7be048274805f"),
    "timestamp" : ISODate("2017-07-04T21:25:18.035Z"),
    "__v" : 0
}

/* 27 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07b7a7d7be048274805c"),
    "timestamp" : ISODate("2017-07-04T21:25:11.131Z"),
    "__v" : 0
}

/* 28 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07b0a7d7be0482748059"),
    "timestamp" : ISODate("2017-07-04T21:25:04.647Z"),
    "__v" : 0
}

/* 29 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07aaa7d7be0482748056"),
    "timestamp" : ISODate("2017-07-04T21:24:58.128Z"),
    "__v" : 0
}

/* 30 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c07a3a7d7be0482748052"),
    "timestamp" : ISODate("2017-07-04T21:24:51.421Z"),
    "__v" : 0
}

/* 31 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c079ca7d7be048274804f"),
    "timestamp" : ISODate("2017-07-04T21:24:44.758Z"),
    "__v" : 0
}

/* 32 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0796a7d7be048274804b"),
    "timestamp" : ISODate("2017-07-04T21:24:38.199Z"),
    "__v" : 0
}

/* 33 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c078fa7d7be0482748045"),
    "timestamp" : ISODate("2017-07-04T21:24:31.414Z"),
    "__v" : 0
}

/* 34 */
{
    "value" : 50.2505315620168,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c078ea7d7be0482748043"),
    "timestamp" : ISODate("2017-07-04T21:24:30.666Z"),
    "__v" : 0
}

/* 35 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0788a7d7be048274803e"),
    "timestamp" : ISODate("2017-07-04T21:24:24.421Z"),
    "__v" : 0
}

/* 36 */
{
    "value" : 51.7415246845763,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0787a7d7be048274803d"),
    "timestamp" : ISODate("2017-07-04T21:24:23.670Z"),
    "__v" : 0
}

/* 37 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0781a7d7be0482748038"),
    "timestamp" : ISODate("2017-07-04T21:24:17.948Z"),
    "__v" : 0
}

/* 38 */
{
    "value" : 53.2018025716146,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0781a7d7be0482748037"),
    "timestamp" : ISODate("2017-07-04T21:24:17.166Z"),
    "__v" : 0
}

/* 39 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c077aa7d7be0482748031"),
    "timestamp" : ISODate("2017-07-04T21:24:10.919Z"),
    "__v" : 0
}

/* 40 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c077aa7d7be0482748030"),
    "timestamp" : ISODate("2017-07-04T21:24:10.721Z"),
    "__v" : 0
}

/* 41 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c076ea7d7be0482748027"),
    "timestamp" : ISODate("2017-07-04T21:23:58.826Z"),
    "__v" : 0
}

/* 42 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0768a7d7be0482748024"),
    "timestamp" : ISODate("2017-07-04T21:23:52.295Z"),
    "__v" : 0
}

/* 43 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0761a7d7be0482748020"),
    "timestamp" : ISODate("2017-07-04T21:23:45.566Z"),
    "__v" : 0
}

/* 44 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c075aa7d7be048274801d"),
    "timestamp" : ISODate("2017-07-04T21:23:38.860Z"),
    "__v" : 0
}

/* 45 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0754a7d7be048274801a"),
    "timestamp" : ISODate("2017-07-04T21:23:32.275Z"),
    "__v" : 0
}

/* 46 */
{
    "value" : 55.6854248046875,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c074da7d7be0482748014"),
    "timestamp" : ISODate("2017-07-04T21:23:25.359Z"),
    "__v" : 0
}

/* 47 */
{
    "value" : 55.8850552677083,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c074da7d7be0482748013"),
    "timestamp" : ISODate("2017-07-04T21:23:25.157Z"),
    "__v" : 0
}

/* 48 */
{
    "value" : 55.8977314920825,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0745a7d7be048274800c"),
    "timestamp" : ISODate("2017-07-04T21:23:17.310Z"),
    "__v" : 0
}

/* 49 */
{
    "value" : 55.9236590972381,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0740a7d7be048274800a"),
    "timestamp" : ISODate("2017-07-04T21:23:12.143Z"),
    "__v" : 0
}

/* 50 */
{
    "value" : 55.9737888383277,
    "detectorType" : "humidity",
    "sensor" : ObjectId("588a427d617fff11d79b304a"),
    "_id" : ObjectId("595c0739a7d7be0482748006"),
    "timestamp" : ISODate("2017-07-04T21:23:05.427Z"),
    "__v" : 0
}

0 个答案:

没有答案