Mongodb Mongoose以最小相对时间差获取最后的X值

Mongodb Mongoose以最小相对时间差获取最后的X值,mongodb,mongoose,Mongodb,Mongoose,我试图得到DB条目的确切数量,它们彼此之间的时差最小。例如,传感器大约每1000毫秒向数据库写入一次数据,但我想读取20个值,最小时间差为1小时 目前,我使用$gt阅读,但这会从数据库中提取72000(20x60mx60s)个条目 然后我对其进行过滤: filterSensorHistory: (data, callback)-> self = @ data = _.sortBy(data, 'timestamp').reverse() lastEntry =

我试图得到DB条目的确切数量,它们彼此之间的时差最小。例如,传感器大约每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

既然您已经意识到,一旦检索到文档,就在代码中这样做是一个坏主意,那么让我们更清楚地了解数据本身的样本是什么样的呢?如果您只是显示了几个文档作为输入,以及您希望从该输入中获得的结果,那么您可能会从其他人那里得到一些反馈,他们可能会以不同的方式看待问题。然后可能会给你一个解决方案。问这个问题的目的是什么,不是吗?让人们更容易提供帮助。你能做的最好的事情就是“更新你的MongoDB”。2.4甚至不再得到官方支持,而且它还存在已知的bug(以及有问题的bug),并且在过去几年中遗漏了所有主要功能的更改。@NeilLunn提供了一些示例数据。基本上,唯一重要的道具是时间戳。我试图更新它,但它运行在raspberry pi 3上,
sudo apt get install mongodb server
告诉我已经有了最新版本。我认为这是因为对32位体系结构的支持在以后的版本中已经被弃用了:嗯?不要相信你读到的一切。32位目标的官方版本每天都在制作,任何说它不受支持的说法都是完全错误的。至于“有人从源代码为您构建”,那么我只需几秒钟就可以找到raspberry pi的至少v3二进制文件,毫无疑问,也有人拥有最新的构建。事实上,这与信任一个三年前有四张选票的帖子相比,3.x似乎可以安装,但它看起来有点粗糙:,到目前为止,2.4.10没有遇到任何问题/限制。
/* 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
}