How to Connect MongoDB to C# the Easy Way

preview_player
Показать описание
MongoDB can be an extremely powerful database choice as well as an incredibly simple database to set up. But how easy is it to talk to MongoDB from C#? It turns out that the answer is very easy. In this video, I am going to start by showing you just how easy it is to connect C# to MongoDB. We will read and write data to a new database within just a few minutes. Then I am going to take you deeper to cover the major operations you will need in the real world including querying, inserting, updating, deleting, and even transactions.

Use the code MKT-COREY at checkout. The 1st 10 to do so get $100 in credit. Everyone gets the free level of Atlas.

Рекомендации по теме
Комментарии
Автор

Dude... Its 2AM. I found this video and got stuck on it. Now I don't know if I'll be able to sleep because I'm already thinking about how to use it. Can't wait to go after your other content on Mongo DB and binge on it. Thanks so much for this video.

DanRegalia
Автор

One year later I am back and this example is just amazing: straight to the point, and started with Winforms and connection to cloud at once. Really simple and well explained. Thank you.

frankberrocal
Автор

Isn't it ineffecient to create a new MongoClient with every request? Or should I go with a singleton if I use this in an ASP project?

kreide
Автор

Creating a "MongoModelBase" has so far been my best approach, where I can keep an Guid Id defined as an abstract property + a CollectionName that i can set in each model, so I dont have to repeat myself when calling the API.

Also I have created a override on ToString() so its easy to debug the models onto console.

carstenberggreen
Автор

i appreciate your effort to give us this valuable information about nosql db and mongodb, relay thank you for your effort

mshmsh
Автор

Thank you for this step by step explanation!

xelaksal
Автор

I am happy you got a partnership with MongoDB, congrats! You deserve it you make great videos

diligencehumility
Автор

Excellent video. I really like this teaching style and working along to create the project!

bradleynixon
Автор

Sir, I have query and i hope you would help me Sir.
Problem is I need to update a document but i want to add the value to the existing value of a field not replace.
eg. Qty was in db like 10 and now i have Qty = 2 so after updating i need it to be 10+2 = 20 not 2.
So what to do? does mongoDB supports sub query like sql or what?
plz help me Sir.

utpalbharali
Автор

hey, just my honest opinion here. first of all, thanks a lot for the video! it is very helpful for a beginner in working with databases. what really helped me in your tutorial is the slow and in-depth explanation of what is going on with all the unknown key words like [BsonRepresentation(...)]. On the other hand I lost you really fast in minute 18 when you started jumping around files really fast.
Overall thank you a lot! for the awesome video!

EitanM
Автор

Thanks, great content. Easy to follow for novice.

VickyGP
Автор

I really loved this video though. The fact it showed data access involving more than one collection was good.
Since I see a lot of repetition, this inspired me to create a source generator.
So what will happen is the cross platform part will not even need the attributes. However, the source generator can make the mongo models, do all the mappings.
Plus can specify collection names for the models so a person is not repeating themselves. Even found I can create a method that takes in an expression for even more flexibility. So a person can focus on the business logic and not the boilerplate code.

andywalter
Автор

Thanks Tim. Learner (and user!) of a good bit of your code. Won't forget it!

bartonmallory
Автор

So helpful, thanks Tim! You've helped me with a lot over the years.

Do you have any other videos covering more mongdb driver functionality? Like, can you sort, skip, limit, etc on Async functions (.FindAsync)? When evaluating a filter, should you make both objects Bson values (BsonValue.Create()), or can you compare a Bson value to any object?

dillonswanson
Автор

Thank you for another wonderful video Tim. Are you planning on doing any videos (or series) on Dapr and Dapr Sidecar?

Aphradity
Автор

Hi! I have a question. You seem to be using the compass to get access to the Database outside of the program, and if I recall, this is just a UI to access it, the database is actually stored in the files of MongoDB itself, right ? If so, that means the database isn't embedded in the C# project, it's just connecting to it from where it is. Is there any way to change this ? To make the database files a part of the project, so that they export with it when (for example) publishing and then using it on another PC ?
I'm asking because I'm working on a game for my studies exam, and I need to store the different Game Objects data inside of a DB, along with some other game elements and mechanics. And I obviously don't want to reinstall MongoDB everytime I switch PC and class for a presentation or just working on it. Maybe that isn't possible, but I still ask. Or maybe there is another way, another Database (More likely NoSql) that exists that could achieve this ?
I hope you'll see my comment!

EFC_Break
Автор

Dont get me wrong, this looks really great in many ways, but I find it a bit scary to save duplicates of data, you’ll end up serving old data everywhere, right? Change your name but your old name will still show in a lot of places. How do you keep your data fresh with this approach?

jonbordon
Автор

@IAmTImCorey Quick question on around 29 minutes in, do you need to initialize a new MongoDB instance for each request? Cant you just cache it?

mayerw
Автор

This was an extremely helpful tutorial, as I'm quite new to this. Quick question unrelated to the subject, but related to Visual Studio:
Why are named parameters formatted as smaller gray fonts in your copy of Visual Studio, but not in mine? (I just installed Visual Studio 2022, all defaults).
Also, why are your constants a *bold* font, but they're formatted the same as any other variable in a default install? Did Microsoft just change the formatting or did you customize yours? I think the way your code is formatted is much easier to read.

GiuseppeGaetanoSabatelli
Автор

Tim, do you have any recommendations where to look for answers on how in insert a List<string> in Mongo? I've watched this and your other tutorial and you create using a model approach but they're all one instance of that model. On the hunt for something where you can insert a list as one document

ld