Transcript from the "Deleting Documents" Lesson
>> Let's go and talk about Deletes. So we've done, inserts, deletes. So we've done inserts, updates up certs. And now we're gonna do a delete. Let's do db.pets.deleteMany. Let's delete all reptiles of breed havanese because there's no such thing as a havanese reptile, so you just give it a query object type reptile and you give it a breed, Hanese, I mean, you're doing a delete many so you wanna be very careful of what you're gonna do with the delete many because you could delete things that you don't want to.
[00:00:46] But in the end, you can see here this ended up deleting 357 documents from our collection. And same thing, there's a delete 1, if you wanna delete just at most one thing. Like if you're only trying to delete one thing, make sure using the delete 1 because delete many it could be catastrophic when you get your query wrong.
[00:01:04] Because it doesn't ask questions. Like you wanna delete all those things off gesture. Like if you just say delete many, I don't know if that's gonna work, not gonna try. I'm just not interested in figuring out if that works. It's like doing RM-RF/ on your computer just don't do it.
[00:01:27] It's like the call of the void as people call that, there's a set of functions called, Find End, so there's, let's see here, find an update, find and replace and find and delete. Let's just do a find a delete I think, I don't have one of my notes here, but, I should be able to find and delete.
[00:01:52] Let's say name Fido, type reptile. Well, I guess that's not, it's find 1, sorry, find 1 and delete. [COUGH] So what this does is that's different from a delete. Notice when we did a delete many here, it just says I deleted a bunch of stuff, it's gone with find one and delete what's it's gonna do, it's actually going to find it, return to that thing, and then also delete it right?
[00:02:40] So in this particular case, I got the Phyto object back from MongoDB, but it also deleted this object. So this object is now deleted in our database. So that's what find one and updates gonna do it's gonna return to you that object or the the document before you update it so you can actually see it before you update it.
[00:03:00] Sometimes that's useful, sometimes you need to see it before you update it or any want that to be the same thing you wanna find it and updated at the same time. So yeah, that's find 1 and update, find 1 and replace, and find one and delete the name of those three different methods.
[00:03:21] Cool, the last one I'm gonna talk about is bulk, right, so this is less useful for you doing our shell. So we're not gonna do it here. But imagine in your code that you're having someone that's going to update their profile and add a pet and remove another pet all at the same time.
[00:03:42] You could write this as three separate queries to MongoDB. And that would be fine Mongo would be just fine with that. What would be easier and a more efficient use is you can do what's called a bulk write, which basically says like, I'm going to queue up three different changes.
[00:03:56] And then I'm going to do those all at the same time. So you, queue up three different queries, and then you flush that bulk all right? And that all goes to the same, over the same connection to MongoDB. Rather than opening three different connections to MongoDB. So, this is just an efficiency thing that you can do to do a bunch of things all at once.
[00:04:15] And that's called bulk right. And you'll find that in all of the Python, Ruby, go drivers that you need that for.