BrandGhost

It's Ridiculously Simple - Insert Documents Into MongoDB With 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 just how easy it is to get writing records into your document store. You'll see how to declare your types on the fly or use records to keep a more consistent schema when using MongoDB with CSharp!
View Transcript
a lot of the times when we're creating applications we default to databases that are based on SQL but as with any technology there are tons of options available to us and today we're going to look at 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 start really simple with mongod DB and look at inserting documents into a document database I'll explain what you need to get set up and just how simple it is to write some code and CP that gets you these records inserted before I start just a quick reminder to check that pin comment for my free Weekly Newsletter and my on dome train so to kick things off yes mongod DB is not a SQL based database mongodb is a document store and what's really cool about this if you're not used to working with document stores is that if you're really comfortable with working with Json you can structure your records basically as Json and insert them into the document database one of the really cool benefits of this is that you don't have a schema that you have to Define in terms of tables but it's very flexible in the sense that each document could technically be a different schema now I don't recommend that you just go dump a bunch of documents with completely random schemas into a collection which is the mongod DB term for what seems like a table that we're used to in SQL databases but having some type of consistency is going to still be helpful the benefit though is that you're not locked into a very strict schema there are a lot of other characteristics that we could Deep dive into comparing nosql databases and SQL databases but we're going to start really simple with just looking at how we can insert records into these databases one of the ways that I really like to use mongodb is especially when I'm just spinning up a quick net application and I don't want to put a ton of effort into creating a big data schema denormalizing it across a bunch of different tables I may get to that but sometimes I just want to be able to store some data and retrieve it and storing it into like a CSV or something just doesn't feel like a good fit with that said let's jump over to visual studio check out the nougat package we need and see how to write some code okay kicking things off here we need this mongod DB driver Nate package I'm currently using version 2.24 but depending on when you're watching this there could be other versions available instead and that's something to keep in mind too because hopefully they don't have breaking API changes I mean that could happen but the apis we're going to look at are very simple I suspect they'll probably stay the same and if they do change they'll be very similar but please take note of that because if you go to install this package way later in the future when you're watching this video it may be a little bit different but the nice thing is to get this all working this is really all that we need to do in terms of our code but one more thing that we're going to want to get setup is a mongod DB instance and I'm not going to give you a full guide on how to go do that but if you check out mongod DB online they give you some free tools and they give you some free hosting if you're using a very small set of data and for me and the stuff that I'm doing this is more than enough for their hosting that's free so uh I'm more than happy to use it but what you're going to want to do is make sure that you can get your cluster set up again if you go create an account on their website you can get walk through this very easily and then they have this tool called mongod DB Compass which is kind of like like a SQL browser but for mongodb databases and you can write queries in here you can browse through the data and you can also modify the records and if you look closely you can even see that there's two curly braces here and some of the filtering and stuff that you can do with mongod DB when you're writing raw queries is that you set it up like Json to be able to query your Json so kind of neat in my particular case and for this tutorial we're going to be looking at a database that I have called Dev leader test and then I've created a collection and I said that's kind of like a table that we have in mongodb it's called YouTube and a collection as you might expect is just a collection of the different documents and that's because we're not talking about a table like in SQL where we have rows and columns but instead just individual documents and that's just something to keep thinking about because the schema that we have to find can be very different across the documents even within a collection the more different that it is between your different records within a collection it's probably more work for you to try and think about parsing that in a consistent way so something to keep in mind but once you have the N get package once you've created your account or you've come up with a different way to go host your database we should be positioned to be able to go query this and if you're using the tools that they provide you you should be able to get a query string created and that's kind of just like SQL where you're going to be able to have your credentials right in the connection string as well as the server information now heading back to visual studio I'm going to jump over to this program and we're going to see just how easy it is to go insert records I have my connection string here just behind this region that I've collapsed but we're going to create a new client and once we have the client we'll ask for the database and that's just going to be Dev leader test just like I showed you for mongod DB compass from there we want to be able to get a collection reference and that's just so that we can go talk about inserting documents into that particular collection but look how little code we need to just go start inserting stuff so this method here that we're calling on line 15 through 20 is called insert one and that's directly on the collection itself and because we're dealing with mongodb and documents we're able to create a new bson document and Bon is her bind Json so we create this Bon document and we get this syntax where we're able to create like a new object that if you've worked with uh these like Anonymous types where you could do like bar if you're used to this kind of thing here where I could say name like I can make a new Anonymous type like this right where if I hover over this you can see that it's like a question mark and then it has XXX for the name but it's really cool because this is just totally Anonymous and that way I can go access it I get channel I get name but we get a very similar syntax like that with the Bon document but I believe this is just using like a collection initializer that you get for a dictionary what's powerful about this though is that if you think about how much work might have to go into creating like a SQL database and coming up with the table schemas we don't even have to think about that we can just basically get a Json structure that we want to work with create them on the fly if we want to and just go insert them so to do this I didn't have to create a new data transfer object or anything fancy like that I really am just able to create it on the Fly these become the property names in my Json structure and then obviously the values to follow but it's really that simple and of course right below that we have an asynchronous version of it as well so if you're writing async O8 code you can absolutely use insert one async I'm just showing that we can use the exact same syntax here and then we just need to await that of course if I go run this we can go see those Json records get input into mongod DB compass and just pressing play here this should finish basically instantaneously there we go and back in mongod DB Compass if I go refresh this we should see these records come up so if I press find here this should go refresh for us and there we go so we have two records that were inserted and that's because I called insert one and insert 1 a sync I used different bson documents but they were structured the same and had the same values so we get this name Channel and subscribers and what we can do from here is actually show it as Json right so as I was saying these are literally just going to become Json records that we have inside of here and this looks very familiar to a lot of us right and one interesting thing to point out is that we have this underscore ID column we didn't go create this this is part of using mongod DB and getting unique identifiers put onto items themselves so this part we did not add directly or explicitly in our code but using mongod DB you are going to get a unique ID associated with the different documents that you put in now this is really helpful if we just wanted to have Dynamic documents that we could go insert and create on the Fly but if you did want to use structure data we can check that out next 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 have a getting started and a deep dive course the getting started one is perfect for people that haven't even programmed 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 app 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 it'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 so moving along if I go back to visual studio you can see that from line 36 to 39 I'm declaring a record and I'm just calling this YouTube info and what we're going to do is declare the structure of this record to match the same thing that we had up here with these Json structures the reason that I'm doing this is just to show you the similarities with declaring these right on the Fly dynamically versus creating a data transfer object explicitly so if you want to make sure that you have a schema maintained and you have that accessible in code and you can work with it more easily personally when I start taking the data structures that I'm working with a little bit more seriously I move away from this Dynamic type of thing to something that's more of a consistent dto to work with but I'm doing that so that these will match the same records that we just saw inserted the way that we do that is when we ask for the collection for mongodb instead of using Bon document here I can use the record type or the different data transfer object type that I want I'm still asking for the exact same collection name right so you can see on line 14 that's commented out I'm still asking for YouTube but all that's changing is the data transfer object that we're working with from there I'm just calling insert one and we could use insert one sync if we wanted to but instead of creating a new bson document again it has to match the type of the collection that we're working with and in this case it's YouTube info I'm just creating a new YouTube info like this if I go run this now we should see one more record that looks the exact same inside of our mongod DB database a quick little run here that's wrapped up and if we jump back to Compass if I go press this find button again we go from these two records that we had to three now and look the structure is identical so again that's not because it had to be the same it's just that I wanted to show you an example of going from a dynamic format with the Json structure into a more structured record type that we could work with so now we've seen how to insert records into a mongod DB database but what happens if we want to do things like deleting and updating those records but when those videos are available you can check them out next here thanks and I'll see you next time

Frequently Asked Questions

What is MongoDB and how does it differ from SQL databases?

MongoDB is a document store, which means it allows you to structure your records as JSON without the need for a strict schema like SQL databases require. This flexibility means that each document can have a different structure, making it easier to work with dynamic data.

How do I set up a MongoDB instance for my application?

To set up a MongoDB instance, you can create an account on the MongoDB website, where they provide free hosting options for small datasets. They also offer a tool called MongoDB Compass, which helps you manage your database visually.

What do I need to insert documents into MongoDB using C#?

To insert documents into MongoDB using C#, you'll need to install the MongoDB driver NuGet package. Once that's set up, you can easily create BSON documents and use methods like 'InsertOne' to add records to your collection.

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