本文介绍了$ unset为空.您必须像这样指定一个字段:{$ unset:{< field&gt ;: ...}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongodb版本3.0.1
猫鼬版本4.0.3

mongodb version 3.0.1
mongoose version 4.0.3

我正在尝试这样做:

groupsModel.updateQ({_id:group._id},{
    $unset:{"moderators":""},
    $set:{"admins":newAdmins}
})

我从catch收到

'\'$unset\' is empty. You must specify a field like so: {$unset: {<field>: ...}}'

但是它不是空的.

moderators不在架构中,这就是为什么我要删除它.

moderators, however, isn't in the schema, which is why I'm trying to remove it.

推荐答案

我无法重现该错误消息,但是如您所见,Mongoose仅会更新架构中定义的字段.但是,您可以通过包含strict: false选项来覆盖该默认行为:

I wasn't able to reproduce that error message, but as you've seen, Mongoose will only update fields defined in the schema. However, you can override that default behavior by including the strict: false option:

groupsModel.update(
    {_id: group._id},
    {$unset: {"moderators": ""}, $set:{"admins": newAdmins}},
    {strict: false}
)

这篇关于$ unset为空.您必须像这样指定一个字段:{$ unset:{&lt; field&gt ;: ...}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:26