gpt4 book ai didi

node.js - Mongoose 清除文档中的整个数组元素

转载 作者:可可西里 更新时间:2023-11-01 09:18:00 25 4
gpt4 key购买 nike

假设我有记录

{
"_id": "3536463525325645",
"name": "james",
"friends": [
"jack",
"john",
"jeff",
"michael"
]
}

我想清除整个数组,所以结果是:

{
"_id": "3536463525325645",
"name": "james",
"friends": [
]
}

我尝试使用 $pull 但无济于事。有人有什么建议吗?

最佳答案

你可以用 update 来做到这一点声明:

Person.update({name:"james"}, { $set: { friends: [] }}, function(err, affected){
console.log('affected: ', affected);
});

关于node.js - Mongoose 清除文档中的整个数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com