How to Connect MongoDB to C# the Easy Way

How to Connect MongoDB to C# the Easy Way

IAmTimCorey

3 года назад

78,010 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@diligencehumility6971
@diligencehumility6971 - 06.02.2022 00:09

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

Ответить
@hqcart1
@hqcart1 - 07.02.2022 01:01

I would like to ask, When you request certain fields from a "Document", Does MongoDB returns the whole Document? or it just returns the selected elements over the wire?

Ответить
@charlesgantz5865
@charlesgantz5865 - 17.02.2022 00:44

Even though this has pretty small models, the data access is already kind of blowing up. If you created say, a UserDataAccess class, a lot of the code would be very similar to the ChoreDataAccess class. This is good for learning, but in production would it be preferable, and possible, to have the Model classes inherit from an interface class, then use reflection to write the DataAccess functions.
So instead of a CreateChore(ChoreModel chore) and CreateUser(UserModel user), you would have a function Create(IModel). Then in the body you would use reflection to get the Model type, UserModel, ChoreModel, etc., and the ChoreCollection string can be figured out from the Model name.

Ответить
@ezekiel6946
@ezekiel6946 - 01.03.2022 21:15

i have a question. lets say you have a many to many relationshio have a class Students and Subjects. and you want to get a list of all the subjects a student is doing. How do you query the sub document subjects (array/list of type subjects )in students?

Ответить
@ezekiel6946
@ezekiel6946 - 01.03.2022 21:39

i have two classes Students and Subjects. A student can be have many subjects and a subjects and have multiple students.

so now that i have created a databse and have added a student object to it and assigned a subject to the student.

how can i get all the subjects that a particular student is assigned to? C# am new to mongodb please help

Ответить
@DanRegalia
@DanRegalia - 07.03.2022 10:55

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.

Ответить
@antonio-star
@antonio-star - 17.03.2022 20:05

Tim, this video made me to try mongodb, thanks. By the way, in old video about MongoDB you have used Guid Id. Is there any difference between string Id and Guid Id?

Ответить
@DavidMartin-vs1dm
@DavidMartin-vs1dm - 31.03.2022 20:37

Quick question: I downloaded the source code and when I ran it, the CompleteChore method I got an error that said I couldn't use transactions in a local MongoDB database. Is this always the case that in order to use transaction I have to be using MongoDB Atlas or is there a setting where I can turn on transactions for a local MongoDB database?

Ответить
@xO_ADX_Ox
@xO_ADX_Ox - 02.04.2022 05:28

I have no regrets by changing from SQL to NOSQL

Ответить
@andywalter7426
@andywalter7426 - 03.04.2022 00:52

Are there any options if a person wants transactions for mongo db but they require completely offline as well. Because there can be an app being built where if somebody loses their internet connection, they can still do their work. The only problem with atlas is if there is an internet outage, then they can't do any work. If it was stored locally, then the person can still do their work.

Ответить
@andywalter7426
@andywalter7426 - 04.04.2022 01:54

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.

Ответить
@ankammadhulika2378
@ankammadhulika2378 - 13.04.2022 19:13

Hey
Is there any series build on mongodb?
If yes link please

Ответить
@scarzix
@scarzix - 14.05.2022 21:50

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.

Ответить
@haunthBranagh
@haunthBranagh - 22.05.2022 15:21

Thank you , for the best lesson👍

Ответить
@Rek-V7
@Rek-V7 - 24.05.2022 05:14

Thanks!

Ответить
@tousiftahir8070
@tousiftahir8070 - 06.06.2022 14:49

session.StartTransaction(); sending an error "Standalone servers do not support transactions.'"

Ответить
@NullzeRT
@NullzeRT - 13.06.2022 00:04

Good video!

I have a question about that order of actions:
1) A reads document
2) B reads document
3) A and B modifies document
4) A writes document
5) B writes document
Will changes of A be discarded? And how to prevent that?

By the way, pass is NotGoingToTellX - mission failed :D

Ответить
@teodordeaconu9035
@teodordeaconu9035 - 02.07.2022 18:49

Does anyone know how to set Model fields as optional?

Ответить
@utpalbharali7586
@utpalbharali7586 - 06.07.2022 13:55

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.

Ответить
@Gannon777
@Gannon777 - 09.07.2022 18:24

This is awesome

Ответить
@mshmsh7046
@mshmsh7046 - 21.07.2022 11:40

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

Ответить
@mayerw6982
@mayerw6982 - 25.07.2022 22:25

@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?

Ответить
@bartonmallory4241
@bartonmallory4241 - 29.07.2022 14:29

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

Ответить
@joslynj.corderogonzalez5954
@joslynj.corderogonzalez5954 - 09.08.2022 04:50

does var result = collection.FindAsync(_ => true); result.ToList(); (separetly)
is the same sync as doing: collecton.Find(_ => true).ToListAsync(); ???
because for me is easier and in a one line.

Ответить
@tree9228
@tree9228 - 02.10.2022 17:09

Out of curiosity wouldnt other devices running your application be able to decompile it and see your login?

Ответить
@frankberrocal
@frankberrocal - 26.10.2022 20:36

Hi good day.

After completing the initial code, I am getting this error on Mac
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET program, but dotnet-/Users/username/Projects/MongoDBApp/MongoDBDemo/MongoDBDemo/bin/Debug/net6.0/MongoDBDemo.dll does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

Any clue what could be the reason?: already installed the Net just in case. but not corrected.

Ответить
@cyberbru1
@cyberbru1 - 09.11.2022 03:15

🎉

Ответить
@VickyGP
@VickyGP - 22.11.2022 16:25

Thanks, great content. Easy to follow for novice.

Ответить
@ldiniz_x
@ldiniz_x - 02.12.2022 18:00

Hello Tim! Great class as always!
I have a question, this may not be the focus of this class but should't we have to put the "using" statement on the ConnectToMongo or in the methods that use the Mongo connection to guarantee that the connection will be closed as soon as we finish the database interaction ?

Ответить
@dillonswanson3985
@dillonswanson3985 - 08.12.2022 08:06

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?

Ответить
@GermanyPropertyMadeSimple
@GermanyPropertyMadeSimple - 29.01.2023 23:57

The easy way 1 hour 10 mins?

Ответить
@GiuseppeGaetanoSabatelli
@GiuseppeGaetanoSabatelli - 08.05.2023 02:43

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.

Ответить
@EitanM
@EitanM - 09.06.2023 14:21

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!

Ответить
@shazawahsh3952
@shazawahsh3952 - 15.07.2023 19:01

Thank you very much

Ответить
@erdalmickeyilkturk
@erdalmickeyilkturk - 10.08.2023 21:09

Dude, with this clarity and putting the interests of the newbies up front... if you write a "Mobile App Development with .NET MAUI and MongoDB" book... you'll be a millionaire 😄

Ответить
@bradleynixon978
@bradleynixon978 - 30.09.2023 03:10

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

Ответить
@frankberrocal
@frankberrocal - 13.12.2023 02:16

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.

Ответить
@026maheshkumars7
@026maheshkumars7 - 25.12.2023 08:37

This guy is posting same videos to get views the same tutorial was published with different name few years back... This kind of content can consfuse many.. Please dont do like this @Corey...

Ответить
@deekshar7197
@deekshar7197 - 01.02.2024 08:29

Hi
var db = client.GetDatabase(databaseName);
When I enter this line I get a red line under GetDatabase. the option is not available in visual studio. Could you please help.

Ответить
@caseypries7559
@caseypries7559 - 06.02.2024 00:07

I really don't like the idea of having data out of sync. If I'm storing a person and then using them in a related object I expect their data to match without my having to manually go back and fix it.

Ответить
@arbitervildred8999
@arbitervildred8999 - 29.02.2024 17:57

"We need to go deeper" achievement re-unlocked

Ответить
@ADLMStudio
@ADLMStudio - 26.03.2024 03:28

thank yo so much for this i really appreciate, but now I will like to know if you have a course that explain how to get the data from Mongodb i.e to get a particular property from the DB and show in WPF form. i will really appreciate a response

Ответить
@pHaace
@pHaace - 02.04.2024 17:39

Even tho this is 2 years old, can you do a sequel and show how to make this connection into a windows form app or a wpf app?

Ответить
@xelaksal6690
@xelaksal6690 - 02.07.2024 17:42

Thank you for this step by step explanation!

Ответить
@FlamingRiot
@FlamingRiot - 23.08.2024 17:37

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!

Ответить