在Mongoose(MongoDB)和NodeJS中获取引用数组的最后一个唯一文档

资深开发者,

我遇到了一个问题,其中我有给定的ID数组,例如

["5dc682a0c2608116f4958d1b","5dc6e25c3945b10174b6a01e","5dcbb142d7fab00cfca2e5ca","5dcbd106d7fab00cfca3058d"]

这些是包含服务器ping的大表的引用。以上是服务器文档的ID。

我的ping模式定义如下:

var mongoose = require("mongoose");

var mPingSchema = mongoose.Schema({
  _id: {
    type: mongoose.Schema.Types.ObjectId,required: true,auto: true
  },cpuLoad: { type: String },diskFree: { type: String },diskMax: { type: String },lastBoot: { type: String },memoryFree: { type: String },memoryMax: { type: String },mDevice: { type: mongoose.Schema.Types.ObjectId,ref: "mDevice" },alert: { type: String,default: null },timestamp: { type: Date,default: Date.now }
});

module.exports = mongoose.model("mPing",mPingSchema);

ping表中充满了成千上万个文档,这些文档在48小时内保持保存,每3秒创建一个新文档。

我创建了以下查询以获取给定服务器的最新n次ping,但是我不确定这是否是正确的方法。看来此查询已完成所需的工作,但我对此表示怀疑。

mPing.find({mDevice : { $in: devices}})
     .sort({'timestamp' : -1})
     .limit(devices.length)
     .exec((err,data) => {
         console.log(data)
      }
eitjsdmwhj 回答:在Mongoose(MongoDB)和NodeJS中获取引用数组的最后一个唯一文档

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3096782.html

大家都在问