In the following snippet, I am able to remove two key fields from a collection with different parameters (i.e. One is 1 and the other is 0).
> i = { name : 'name', age : 25, gender : 'female' };
{ "name" : "name", "age" : 25, "gender" : "female" }
> db.users.insert(i)
> db.users.find()
{ "_id" : ObjectId("4e8b5b5e654f46ccc304e44e"),
"name" : "name", "age" : 25, "gender" : "female" }
> db.users.update({ name : 'name'},
{$unset : {age : 1, gender : 0}}) // check here
> db.users.find()
{ "_id" : ObjectId("4e8b5b5e654f46ccc304e44e"), "name" : "name" }
>
Reference: MongoDB $unset
Question> What is the usage of 1 in the following manual?
{ $unset : { field : 1} }
As far as I know, this is simply due to JSON/BSON syntax. There has to be a value, but it doesn't matter what the value is. In the MondoDB documentation they typically use 1.
MongoDB : Update Modifier semantics of "$unset"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With