BrandGhost

Be Careful! Easy Document Deletion From MongoDB Using C#

We're all so accustomed to working with different SQL databases... But have you spent much time working with document databases? In my development more recently, I've been trying to make sure I spend some time working with MongoDB in C# so that I have more exposure to these different tech stacks. In this MongoDB tutorial, I'll show you how to delete documents from MongoDB databases from C#. You'll want to make sure you understand how filtering works in MongoDB first so that you can properly target documents for deletion! With your filtering knowledge, we can get our MongoDB deletes done with some simple C# calls.
View Transcript
more recently I've been building C applications and trying to use other database formats and I'm very used to using SQL but I wanted to spend a lot more time with mongod DB hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video we're going to be looking at using C with mongod DB to delete records out of mongod DB now if you haven't watched my previous video already about looking at filtering and querying you should check that out right now come back and watch this one because we're going to be using some of the filtering techniques to look at which records we want to delete if you can't filter properly it's going to be hard to delete the right record and you want to be careful when you're deleting stuff a quick reminder to check that pin comment for a link to my free weekly software engineering newsletter and my courses on D drain but that said let's jump over to visual studio and look at deleting stuff okay so everything that you see at the top here is covered in previous videos which I have linked you to already but if you don't want to watch the previous video on filtering it's all right here and pretty straightforward for the basic stuff so we are starting with an empty filter on 9 20 and then we're combining that with this filter Builder equal filter where we're going to look for documents with a name property equal to my name Nick centino now if you're doing more advanced filtering you may want to check that video out cuz it will explain how you can combine these things to do something a little bit more advanced now when it comes to deleting records from a collection in mongodb it's really straightforward we have this delete one method that we can use if you want to go delete a single record that's going to match this filter and of course there's async variations of all of the different methods that we have with the mongod DB driver in C so if you're using async code and you're using async await you can go use those and make sure that you're awaiting these things in this particular case and for the rest of the examples I'm just going to show the synchronous versions but like I said there are a sync versions as well so before I go run this I want to show you what I have in the database already and that way we can see these things get removed and I'm going to have to take some time in between to go reinsert them so we can keep running the other examples so jumping over to mongodb Compass you can see that I have these three records in here I'll just refresh so I have three two of them have name Nick centino and one has name Dev leader and going back to the code looking at the filter we have this is a filter that says go delete the records that match name niit centino I'm doing this example on purpose because delete one is the method that we're calling right but we have two records that match this filter so what should happen does it delete one or does it delete all of the records that match this filter well I will go run this and then we'll see what it prints so it said in the code that it did delete only one record right so that's good news but is it true let's check out compass and see when I refresh this it did in fact only remove one of them that had Nick centino in the name now I believe that it will do this in order of the IDS that it's created or the default sort order but I wouldn't Bank on it I think if you're trying to to delete stuff you want to make sure that you're being very careful about what you're deleting of course so if you're writing filters that maybe could match too many things you may want to reconsider the filter that you're writing to be very specific about the records you care about and if you're basically saying I want to delete anything that matches this filter we probably don't want to use delete one there are other methods we could use and those other methods are delete many so this is probably what you would want to use if you wanted to get rid of everything that had Nick centino in the name so I'm going to go back to Compass I'm going to change both entities to have name Nick centino and we should see that this one deletes two records okay here's a quick shot before we go back to the code both documents have named ni catino now if I go run this we can see deleting records using delete many and then it deleted two which should mean if I go back to Compass and refresh this all of it goes away so again two different variations I didn't touch the filter I would say if your filter is supposed to be covering many things to delete use delete many and if your filter is not supposed to and you're using delete one you probably are going to have some weird Behavior because some things are going to be left behind the last example that I want to walk through is this other method that we have called find one and delete this one's interesting because it's very similar to the first one we saw where we were using delete one this is find one so again you want to think about a filter that is hopefully matching one thing because it will in this case only match one even if your filter matches many but it's going to find one give it back to us and delete it at the same time so this is really helpful if you're deleting something because you want it out of the database but you want to know what that record was so instead of doing like a query to pull it back and going cool I got the data let me go delete it now you could do it in one step where it will delete it and give you the result back that you deleted I've just gone ahead and recreated the data in here so we can see that we have three records once again I'm going to go run the code and we should see that we get something printed back to the console right so we get one object that comes back deleted record is going to be this ID here so I'm going to go show you in compass now that we don't have this ID of the two objects that are remaining so back in compass if I go find right we only have two here and this one ends in 58 BF and this one ends in 553a this one that we had was 58 be do we see that anywhere on our screen and I think the answer is no right 58 BF so that's not the same as be and this one's 55 it's not even the same at all so this is an example like I said if you wanted to delete something and get that result that you deleted so remember when you're deleting things you want to be very careful with your filter because of course if you're mixing and matching your filter that could match many things and only deleting one that might give you some weird Behavior once that stuff's deleted from the database good luck getting it back right so you want to be careful with this kind of thing and you have a bunch of different options when you're building up the filters like I said it's a pretty straightforward syntax which makes this awesome to work with but your filters can get very complex if you want to continue to learn about mongod DB and working with C when the next videoos ready you can check it out here thanks and I'll see you next time [Music]

Frequently Asked Questions

What is the difference between delete one and delete many in MongoDB?

The delete one method will remove only a single record that matches the filter you provide, while delete many will remove all records that match the filter. If you're trying to delete multiple records, you should use delete many to ensure that all relevant records are removed.

How can I ensure I'm deleting the correct records in MongoDB?

To ensure you're deleting the correct records, it's crucial to write specific filters that accurately target the documents you want to remove. If your filter is too broad, you might end up deleting more records than intended.

What does the find one and delete method do in MongoDB?

The find one and delete method allows you to find a single document that matches your filter and delete it in one step. This method returns the document that was deleted, which can be useful if you want to know what data was removed.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload