C# MongoDB Beginner's Guide: How To Update Documents
March 29, 2024
• 436 views
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 update documents in 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 updates! With your filtering knowledge, we can get our MongoDB updates done with some simple C# calls.
View Transcript
if you've been using mongod DB in your C applications there's guaranteed been a time where you've inserted some data and said man I wish I could go update that and fortunately for you you can because the apis that we have access to with the mongod DB C driver are very straightforward hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to walk you through some of the very simple apis that we have to use with the mongod DB C driver for updating records we're going to see how we can filter on the right things and make sure that we're updating what we want this is geared for beginners trying to get started with using mongod DB and C so we're not going to look at very Advanced Techniques here a quick reminder to check that
pin comment for a link to my free Weekly Newsletter and my courses that are available on D train let's head over to visual studio and check out the code all right so the first entire part that I'm highlighting here is code to get set up with mongod DB in C and make sure that we can connect and be able to filter on things and I'm highlighting this because I've created previous videos will walk you through this you can check that out now come back and then this might make a little bit more sense or quickly just to explain we start off with an empty filter and we're just combining that empty filter with a filter that's going to match when a name property on a document is equal to my name this could easily be replaced by this line here on line 20 without the
empty filter but I like breaking it out when I'm writing more complex filters now what's new compared to those other videos is this update Builder but we create this in a very very similar way so we ask this Builder static class for the update class and then we store that into this update Builder variable again this assignment is not necessary but if you're using more update Builder calls here instead of calling that static class many times we can just call it once and in my opinion it makes the code a little bit easier to use instead of repeating this a handful of times in this example we're only going to be looking at calling set on one property so we're going to change the name from Nick centino to devader now at this point we're not actually calling the update anywhere so I just wanted
to show you how we structure this so this is going to find the records that we want to update and this is how we want to update them so two different parts right we need to pass this into something else which I'll show you in just a moment but before I do I wanted to show you Compass where this data is I'm going to refresh the data and inside you can see that I have this collection that has three items inside of it there's three that all have name as Nick centino and that's important because when we go to call this method we should be able to see what's happening so the example code that I have here from line 25 to 34 is calling update 1 update 1 is taking in that filter that we created and the update method that we created from
the update Builder again filter is picking which objects or documents we want to go update an update is explaining how we want to go update them but something very important that I want you to note is that this method is called update one it's called update one but we have three documents that are going to match this filter that I created right all three documents had the name Nick centino so in the previous videos that I've created especially where I was talking about deleting we want to make sure that when we're filtering we're filtering accordingly so make sure your filter matches and if you don't want to just update one but you want to update more than one because your filter is supposed to update more than one we'll see that in just a moment so when I go run this we should be able
to see the result of it on the console and we can see that it matched one and modified one that comes back as part of the result when we call that update one method so if we jump back over to Compass if I refresh this data you can see that that first one the name changed to Dev leader the other two are untouched right they still say name is Nick centino and yes all three technically match the filter but only the first one was updated because we called update one of course if we want to go change this around a little bit we can call update many and instead of just updating the single record it should update all of them that match the filter at this point though two of them match the filter and one of them is already updated to say Dev
leader so if I go run this now we should see the correct counts come up so two right there's two things that were updated here that's good news if we go back over to Compass if I refresh this note the names are still Nick centino but when I refresh they go to Dev leader now so that means that we were able to update multiple records calling the right method and a quick note right I didn't change these two things at all all that I did was change the method from update one to update many filter and update calls scrap that filter and update objects were left unchanged just the method changed okay building on a more complex example here if we were calling update many which could affect multiple documents as we just saw if we wanted to still make sure that we could update
only a single record if we could pick the ID that we wanted to work with we could go Target that one specifically in this particular case it would probably make more sense to use update 1 but I just wanted to show you that when you're filtering properly and in this case picking a specific document with this exact ID we could in fact update it regardless of update many or update one again this is probably not the right method you want to use I just want to illustrate that this can filter Down based on what your filter definition is up here when we go run this it should only go touch one record and just to show you I have this first record in here this is the ID that we're working with and you'll note too that this is new object ID so this is
built into mongodb to give us this object identifier but I'm also starting with a new empty filter just please note that I'm not redeclaring it I'm just reassigning it it both the filter and the update so this one should set the name to very special for that first record all right heading back over to Compass we can see that this one that has the ID that we were referring to right here has had the name updated to very special and just to show you if I move this down out of the way we can see this ends in 5975 right here and this one in compass also ends with the same thing so it only updated that one if we look at the other two record here they're still left untouch I'll refresh and you can see same thing right name is updated to very
special only for that one despite using update manyu and before moving on I just wanted to take a quick note to show you my Dome train courses that I have available now I do have two that are focused on C which you can see right here and right here so we have a getting started and a deep dive course the getting started one is perfect for people that haven't even program before I'll teach you all the basics you need to get up and running and working with c and the Deep dive builds directly on top of that so that you have more experience working with c and you feel a lot more capable working with the language to go develop applications together both of these courses are just around 11 and 1/2 hours of all of the basics of C that you'll need to get
up and running of course building on top of all of that I have my refactoring course that's available and this is going to teach you different methods that you can use to refactor code clean up your code base and make sure that you're not trapped working with spaghetti Legacy code if you like the style of my YouTube videos and the way that I teach I think these courses will be perfect for you okay and for our final example we're going to look at one more method called find one and update this one's really cool because we're able to update a particular record again it's find one so make sure that your filter is only matching one or else you might have some unintended side effects here so in this case I'm using this same type of filter that's going to match the exact ID so
this is the proper use of updating one and the difference with this find one and update is that we're going to get that result given back to us before it was updated another difference here is I'm just changing the name so we can keep track of it it's going to change it to Dev leader rocks let's go ahead and run this and you'll note that what's printed out to the console is the object that we were targeting right the one that ended in 5975 the name used to be very special right that's not what we just updated it to it's what the value was before we updated it just an important note so that means if I go back to Compass if I refresh this right we're looking at this first record here that ends in 5975 if I refresh that got updated to Dev
leader rocks and again going back to the console this is what the record used to be where it said very special for the name and of course now the name is Dev leader rocks so find one an update in the code here is giving you that value back before it updates it so we have a handful of different ways that we can update records rongo DB from C and we got to see updating a single record updating multiple and then updating one but also getting that reference back to the object before we updated it but in this video series so far if you've watched the previous ones up until now we haven't really talked about performance or benchmarks at all so if you're interested in learning more about that when those videos are ready you can check them out here thanks and I'll see you
next time
Frequently Asked Questions
What is the difference between update one and update many in MongoDB?
The difference is that update one will only update the first document that matches the filter, while update many will update all documents that match the filter. In my example, when I used update one, only the first document was updated, but when I switched to update many, all matching documents were updated.
How can I ensure I'm updating the correct document in MongoDB?
To ensure you're updating the correct document, you should create a precise filter. For example, if you want to update a document by its ID, use a filter that specifies that exact ID. This way, you can target the specific document you want to update without affecting others.
What does the find one and update method do in MongoDB?
The find one and update method allows you to find a specific document and update it, while also returning the document's state before the update. This is useful if you want to see what the document looked like before making changes.
These FAQs were generated by AI from the video transcript.