//you can use update with $set or $push based on whether you want to update one element in array with override/update or push another element.
> db.rooms.find();
{ "_id" : 1, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "raj" ] }
> db.rooms.updateMany( {}, {$set:{"room":["john"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john" ] }
> db.rooms.updateMany( {}, {$push:{"room":["mike"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ] ] }
> db.rooms.updateMany( {}, {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
>
the push will add a field in the document if it does not exist, so a specific check for field room is not required.
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
> db.rooms.updateMany(
... {},
... {$unset:{"user":""}}
... );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 0 }
> db.rooms.updateMany( {}, {$unset:{"room":""}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3 }
> db.rooms.updateMany( {}, {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "mike" ] }
>