Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...