Complete Intro to Databases

Deleting Documents

Brian Holt

Brian Holt

SQLite Cloud
Complete Intro to Databases

Check out a free preview of the full Complete Intro to Databases course

The "Deleting Documents" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to delete multiple objects from a MongoDB database, and how to find and delete a specific query object based on a given characteristic within the database.


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.

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.

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.

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.

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?

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.

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.

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.

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.

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.

And that's called bulk right. And you'll find that in all of the Python, Ruby, go drivers that you need that for.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now