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

后端 未结 3 2231
天涯浪人
天涯浪人 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:30

    If you want remove one element from an array use this

    User
    .update( 
      {_id: req.user._id}, 
      { $pull: {posts: req.body.post_id } } 
    )
    .then( err => {
      ...
    });
    

    Here the documentation

提交回复
热议问题