Mongoose: Remove object from array based on ID (Cast Error)

后端 未结 3 2228
天涯浪人
天涯浪人 2021-02-09 15:27

I have a model that looks like this:

mongoose.Schema({
  username: String,
  posts: [{ type: Schema.Types.ObjectId, ref: \'Post\' }]
});

I have

3条回答
  •  暖寄归人
    2021-02-09 15:34

    In order to make a research with an id in a collection you need to create a new ObjectId and then pass the id into it.

    app.delete('/post', function(req, res) {
        User.findOne({ _id: _id: ObjectId (req.user._id)}, function(err, result) {
            result.pull({ _id: ObjectId (req.body.post_id) });
        });
     });
    

提交回复
热议问题