5. Inserting, Updating, and Deleting Documents
Activity 5.01: Updating Comments for Movies
Solution:
Perform the following steps to complete the activity:
- First, update the
movie_id
field in all three comments. As we need to apply the same update to all three comments, we will use thefindOneAndUpdate()
function along with the$set
operator to change the value of the field:db.comments.updateMany( Â Â { Â Â Â Â "_id" : {$in : [ Â Â Â Â Â Â ObjectId("5a9427658b0beebeb6975eb3"), Â Â Â Â Â Â ObjectId("5a9427658b0beebeb6975eb4"), Â Â Â Â Â Â ObjectId("5a9427658b0beebeb6975eaa") Â Â Â Â ]} Â Â }, Â Â { Â Â Â Â $set : {"movie_id" : ObjectId("573a13abf29313caabd25582")} Â Â } )
Using the update command, we find three movies by their
_id
, providing their primary keys using...