Hello all
In the last post (here), I have talked about read and list operations, or the "R" from CRUD.
Today, I'm going to talk about update and delete operations in Mongo DB, the "U" and the "D" in CRUD.
But remember, I'm talking about basic operations. As we know, the Mongo DB world is much bigger than this post.
Let's get started!!!
We often use the operator "set" to execute the update.
-- Update a single data where codigo=14
db.movies.updateOne({ codigo: 14 },{ $set: { year: "2020" } })
-- Update using operator - We are updating the year on documents where codigo >= 97
db.movies.updateMany( { codigo: { $gte: 97 } } ,{ $set: { year: 2025 } })
-- Update the document where codigo=97, using the exclusive operator to increment the original value
db.movies.updateOne( { codigo: 97 } , { $inc: { codigo: -10 } } )
-- We can create a new "column" call "watched" and update all documents on collection with true, as well. The famous "Update without where" 🥶.
db.movies.updateMany({ }, { $set: { watched: true } })
Ok, the basic update operations are easy!!!
And, what about the delete operations?
They're very easy too!!!
Owwwwwww yes, it's easier than update, right...
With this post, I'm finished the posts with CRUD operations on Mongo DB.
I hope that it helps you to start on with Mongo DB.
The next post will talk about Index.
See you!!!
Regards
Mario