UPDATEMANY quey not showing any change in collections

I ran the following query but can’t see the result in database.

Please read https://docs.mongodb.com/manual/reference/operator/update/unset/

Note the following from the above documentation

The specified value in the $unset expression (i.e. "" ) does not impact the operation.

So { $unset : { rated : “Rahul” }} does nothing because you just did { $unset : { rated : “” } }, the field is already unset.

But,in the last query i did {$set:{rated:“Rahul”}}

Yes, you did and modifiedCount was 1599.

Hi @Rahul_14779,

Let’s analyse all the three queries in detail which you have run.

  1. db.movieDetails.updateMany({rated:null},{$unset:{rated:""}})

    It basically means, find all the document where the rated field is null and then $unset (delete) all those fields from the matched document.

    [O/P] - {"acknowledged": true ,"matchedCount": 1599,"modifiedCount": 1599}

  2. db.movieDetails.updateMany({rated:null},{$unset:{rated:"Rahul"}})

    [O/P] - {"acknowledged": true ,"matchedCount": 1599,"modifiedCount": 0}

    Here, the value of modifiedCount field is 0 because, the rated field has already been unset after running the query #1.

  3. db.movieDetails.updateMany({rated:null},{$set:{rated:"Rahul"}})

    [O/P] - {"acknowledged": true ,"matchedCount": 1599,"modifiedCount": 1599}

    This query basically means that for all those documents where the rated field is null/non-existent, replace the value of the rated field by Rahul.

Now, one important thing that you need to know is that that the specified value in the $unset expression (i.e. "" / "Rahul" ) does not impact the operation.

You can verify this by running this query.

db.movieDetails.updateMany({rated:"Rahul"},{$unset:{rated:"The value in this field does not matter"}})

Hope it helps!

If you still have any query then please feel free to get back to us.

Happy Learning :slight_smile:

Shubham Ranjan
Curriculum Support Engineer