从猫鼬的内部架构中删除对象?

时间:2021-04-22 21:32:10

标签: mongodb mongoose mongodb-query mongoose-schema

如何从 mongoose 的内部架构中删除对象? 我尝试从假日模式中删除评论,这是假日模式:

const holidaySchema = new mongoose.Schema(
  {
    comments: [commentSchema],
  },
)
const Holiday = mongoose.model("Holiday", holidaySchema);


export default Holiday;

这是评论模式:


const commentSchema = new mongoose.Schema(
  {
    action: { type: String },

    time: { type: String },

    name: { type: String },
    image: { type: String },

    content: { type: String },
    rating: { type: Number },
  },
  {
    timestamps: true,
  }
);


我尝试以这种方式从holidaySchema中删除特定评论:

holidayRouter.delete(
  "/:id/comments/:commentId",
  isAuth,
  expressAsyncHandler(async (req, res) => {
    const holiday = await Holiday.updateOne(
      { _id: req.params.id },
      { $pull: { comments: { _id: req.params.commentId } } }
    );
if(holiday){
  console.log(holiday);
}
  })
);


控制台:

enter image description here

这不起作用,你知道我做错了什么或者我应该怎么做吗?

谢谢

0 个答案:

没有答案
相关问题