Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose在嵌套模式中查找_Javascript_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

Javascript Mongoose在嵌套模式中查找

Javascript Mongoose在嵌套模式中查找,javascript,node.js,mongodb,mongoose,mongoose-schema,Javascript,Node.js,Mongodb,Mongoose,Mongoose Schema,在mongoose中,我们正在嵌套模式中进行深入搜索,但没有取得多少成功。每次运行此函数时,都会返回一个空数组 function findAlarms(lastUpdate = new Date(0), record = Record) { // For docs on find http://mongoosejs.com/docs/queries.html return record .find({ // Date due must be less than "n

在mongoose中,我们正在嵌套模式中进行深入搜索,但没有取得多少成功。每次运行此函数时,都会返回一个空数组

function findAlarms(lastUpdate = new Date(0), record = Record) {

  // For docs on find http://mongoosejs.com/docs/queries.html
  return record
    .find({
      // Date due must be less than "now"
      'documents.alarm.date_due': {
        $lte: Date.now(),
      },
      // Must be greater than the last update and less than "now"
      'documents.alarm.date_reminder.reminder': {
        $gte: lastUpdate,
        $lte: Date.now(),
      },
    })
    .populate('documents')
    .exec();
}
对我们的模式进行了详细总结,如下所示:

const RecordSchema = new mongoose.Schema({
  documents: [
    {
      type: Schema.Types.ObjectId,
      ref: 'Document',
    },
  ],
});
const DocumentSchema = new mongoose.Schema({
  alarm: {
    date_due: { type: Date },
    date_reminder: [
      {
        reminder: { type: Date },
      },
    ],
  },
});
我们的文档模式类似地总结如下:

const RecordSchema = new mongoose.Schema({
  documents: [
    {
      type: Schema.Types.ObjectId,
      ref: 'Document',
    },
  ],
});
const DocumentSchema = new mongoose.Schema({
  alarm: {
    date_due: { type: Date },
    date_reminder: [
      {
        reminder: { type: Date },
      },
    ],
  },
});
此搜索不返回匹配的元素,即使我们知道有匹配的文档。如果我们修改
findAlarms
方法以使用文档架构:

function findAlarms(lastUpdate = new Date(0), document = Document) {
  // For docs on find http://mongoosejs.com/docs/queries.html
  return document
    .find({
      // Date due must be less than "now"
      'alarm.date_due': {
        $lte: Date.now(),
      },
      // Must be greater than the last update and less than "now"
      'alarm.date_reminder.reminder': {
        $gte: lastUpdate,
        $lte: Date.now(),
      },
    })
    .exec();
}
它将返回我们所有匹配的文档。然而,有记录对于我们的需要是至关重要的。现在,我可以使用hack,然后使用返回的
文档数组查找记录


尽管如此,我还是很想知道是否有一种方法可以让我们直接使用记录,因为添加额外的步骤感觉非常粗糙,而且这个操作每5分钟运行一次,所以我希望在可能的情况下提高效率

您说:
此搜索不返回匹配的元素
并且
它将返回所有匹配的文档
抱歉,但这没有意义。第二个示例使用文档模式,而不是记录模式。