$ unset is empty. You must specify the following field: {$ unset: {<field>: ...}}

mongodb version 3.0.1
mongoose version 4.0.3

I am trying to do this:

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

And I get MongoError from catch indicating

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

But it is not empty.

moderators , however, are not part of the scheme, so I am trying to remove it.

+5
source share
1 answer

I could not reproduce this error message, but, as you saw, Mongoose will update the fields defined in the schema. However, you can override this default behavior by enabling the strict: false option:

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

All Articles