Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

mongodb version 3.0.1
mongoose version 4.0.3

I'm trying to do this:

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

And I'm getting a MongoError from the catch stating

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

But it isn't empty.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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}
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...