segunda-feira, 16 de janeiro de 2023

[Mongo DB] - Basic update and delete operations (or "U" & "D" from CRUD)

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!!!

The references are here and here.

We started with update operation.

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!!!

-- Delete one document where codigo=98
db.movies.deleteOne ( { codigo: 98 } )

-- Delete many documents where trilogy = "Procurando Nemo"
db.movies.deleteMany({  trilogy: 'Procurando Nemo' })



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 

 







Postagem em destaque

[ORACLE] How to move a datafile to a new location in ASM.

Hello everyone! I hope you're doing well! Today, I needed to move a datafile to another ASM Disk Group. The process was simple, but reme...