本文介绍了如何从模型的数组中删除特定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的带有 watched
和 watchLater
数组的用户模型:
This is my user model with watched
and watchLater
arrays:
const UserSchema = new mongoose.Schema(
{
email: { type: String, required: true, unique: true },
watched: [{ type: Number }],
watchLater: [{ type: Number }],
},
{ timestamps: true },
)
我有这个功能,我想从 watchLater
中删除 id 并添加到 watched
:
I have this function where I want to remove id from watchLater
and add to watched
:
async addToWatched(id) {
const _id = this.getUserId()
return await this.store.User.findByIdAndUpdate(
{ _id },
// remove id from watchLater and add to watched,
{ new: true },
)
}
我该怎么做?
推荐答案
阅读了一些文档.这似乎有效:
Been reading docs a bit. This seems to work:
return await this.store.User.findByIdAndUpdate(
{ _id },
// remove id from watchLater and add to watched,
{ $pull: { watchLater: id }, $addToSet: { watched: id } },
{ new: true },
)
这篇关于如何从模型的数组中删除特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!