Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.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
Node.js MongoDB-从模式模型中仅查找布尔值为false的元素_Node.js_Mongodb_Schema_Mongoose Populate - Fatal编程技术网

Node.js MongoDB-从模式模型中仅查找布尔值为false的元素

Node.js MongoDB-从模式模型中仅查找布尔值为false的元素,node.js,mongodb,schema,mongoose-populate,Node.js,Mongodb,Schema,Mongoose Populate,我正在解锁MongoDB和NodeJS 我试图找到所有任务,并在仪表板上显示done:false。 我按从最新到最旧的顺序进行排序,发送和显示的限制为4,但它发送所有任务,我希望只发送带有done:false(这意味着任务未选中:done:true)的任务,但我不知道如何将其放入populate的选项中 我的任务模式模型: let mongoose = require("mongoose"); let taskSchema = new mongoose.Schema({ taskti

我正在解锁MongoDB和NodeJS 我试图找到所有任务,并在仪表板上显示done:false。 我按从最新到最旧的顺序进行排序,发送和显示的限制为4,但它发送所有任务,我希望只发送带有done:false(这意味着任务未选中:done:true)的任务,但我不知道如何将其放入populate的选项中

我的任务模式模型:

let mongoose = require("mongoose");

let taskSchema = new mongoose.Schema({

    tasktitle: String,
    taskcomment: String,
    project: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Project'
    }],
    user: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    created: {
        type: Date,
        default: Date.now
    },
    done: {
        type: Boolean,
        default: false
    }
})

module.exports = mongoose.model('Tasks', taskSchema);
exports.render_pending_tasks = (req, res) => {
let userid = req.user._id;

  User.findById(userid).populate({ path: 'tasks', options: { sort: { _id: -1 }, limit: 4 } })
      .exec((err, tasks) => {
         if (err) {
             console.log(err);
            } else {
                res.send(tasks);
            }
        });
};
我的任务控制器:

let mongoose = require("mongoose");

let taskSchema = new mongoose.Schema({

    tasktitle: String,
    taskcomment: String,
    project: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Project'
    }],
    user: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    created: {
        type: Date,
        default: Date.now
    },
    done: {
        type: Boolean,
        default: false
    }
})

module.exports = mongoose.model('Tasks', taskSchema);
exports.render_pending_tasks = (req, res) => {
let userid = req.user._id;

  User.findById(userid).populate({ path: 'tasks', options: { sort: { _id: -1 }, limit: 4 } })
      .exec((err, tasks) => {
         if (err) {
             console.log(err);
            } else {
                res.send(tasks);
            }
        });
};

您可以在populate函数中使用
match
选项

User.findById(userid)
 .populate({
  match: { done: false },
  path: 'tasks',
  options: { sort: { _id: -1 }, limit: 4 }
})

使用匹配选项
{match:{done:false}}
耶!就这样!非常感谢您的时间和帮助!可能重复的