Video is uploaded 5 minutes ago and someone already clicked a dislike. Hey man, you even did not watch this!!! Thank you Tim for this tutorial. Thumb up!
Yeah, I saw that. The funny thing is that RUclips cares about engagement. The more engagement a video gets (likes, dislikes, comments), the higher they rank it so technically, that person is helping me just a bit. :-)
Thank you for a good introduction, Tim. Been using MSSQL for many years and knows it very well. For me this was a first look at NoSQL, and it looks very very interesting! At first it feels very uncomfortable with the loose data structure but it really makes a lot of sense. Just the fact that the termonology is "documents" rather than "tables" makes so much sense; love that we're simply just storing the data as it actually is instead of joining tables and all that.
Hello Tim, You have a logical error in this video starting from the time position 1:00:00, when you tried to bring and use new NameModel class. It is working for you because you deleted the last record that has PrimaryAddress just before you introduced NameModel. If you actually have couple existing records in DB, some of them have only FirstName and LastName (and Id for sure) and some have FirstName, LastName and PrimaryAddress, you won't be able to use NameModel as you just did. You will have unhandled exception in the line: return collection.Find(new BsonDocument()).ToList(); saying: 'Element 'PrimaryAddress' does not match any field or property of class MongoDBDemo.NameModel.' So you cannot "narrow down" your objects so easily if you actually have additional data in the MongoDB, you will have to use some conversion.
I forgot to show that. Sorry about that. The solution is to put a [BsonIgnoreExtraElements] on the top of your model and it will work. I meant to show it. Thanks for pointing it out.
0:00 - Intro 1:27 - Setup: Mongo DB Community Server installation 8:06 - Creating Demo Core Console App 9:35 - MongoDB NuGet 11:04 - Mongo CRUD 12:50 - "Connection string" 14:50 - Connecting to database 17:13 - Data storage in MongoDB 24:39 - Inserting data in database 28:07 - Changing data set structure 30:45 - Inserting different data set in the table and benefit of NoSQL 36:52 - Retrieving data form database 42:00 - MongoDB Query 48:40 - Update and delete methods 54:28 - Insert or Update record 59:00 - Delete record 59:42 - Returning different data model from database record 1:02:55 - Demo code clean up 1:03:20 - MongoDB when and why? 1:15:15 - Summary and concluding remarks
Mr. Corey, thank you. I am accepted to an internship position in an international company called Exadel. The knowledge you are sharing in RUclips is priceless. I am still learning from your content and plan to one your paid courses once I receive my salary as a .net developer. Your student from Uzbekistan :)
UpsertRecord with the ReplaceOne, 52:39, is obsolete in new Mongo installs. Need to replace "new BsonDocument..." with a filter on the ID public void UpsertRecord(string table, Guid id, T record) { // Create the collection var collection = db.GetCollection(table); // Create the filter var filter = Builders.Filter.Eq("Id", id); // Replace the record via an upsert // Upsert is an update if the filter finds something or // an insert if there is nothing matching collection.ReplaceOne( filter, record, new ReplaceOptions { IsUpsert = true }); }
I am learning NoSQL for the first time and its the best video have found it very informative and in very understandable tone. MongoDB is very good NoSQL option with .NET Core. Thanks
Thanks for this. It really helped me understand MongoDB and when to use it. I've been using MSSQL for 20 years. I don't see using mongodb for anything at work yet, but I'm testing it with a app at home.
This is a great Video, I've done a introductory course in MongoDB, but as you said it right "this is my new tool... then now what?" even they in that course just tell you about using it with console and Atlas so this is a very good piece of material to integrate it with C# Thanks a lot.
As usual, a very, very nice video. Got a project on this noSQL thing and had no idea where to start. Was most-delighted when I saw you did a video because I knew it was going to be the best place to start. Took me through all the CRUD operations and can't believe how simple it is in C#. The Cosmo Db in Azure is a "fake" MongoDb. It offers various "API"s, one of them is Mongo. So, for the most part, you send it normal Mongo instructions and it responds, just like Mongo. Cool. And, only costs about $5-$6 a month to get started. Connection String inserts just like you said it would in your video. I am a SQL Server bigot but am most-happy about being able to add this to my resume and quiver. Yeah, I can see the positives. Did have 2 questions. One, more complex filter usage for query, upsert and delete. Two, can you remove a property in a "document" once added. Not the value of the property but the property itself. Say you added something and company policy has changed and that property, from each record ("document") has to be removed for legal reasons. Also, I'm curious, it's so fast? There has to be indexing then. Is it/are they clustered or non-clustered? Would be very surprised if there were no indexes. I found the constant reference to Models interesting. So non-structured and yet the model usage. Not sure how to wrap my head around that but, coming from a very-structured world, I find this really easy going. Thanks again Tim. All the best. Awesome.
I will be doing other videos shortly using MongoDB. As for removing the property, you should be able to pull down the record and then save it without the property. As for the speed, its speed comes primarily from the fact that all of the data is together instead of pulling from separate locations. One quick note: I originally thought you could drop the units to 100 for MongoDB but you cannot so the smallest database you can get is 400 units, which costs $25/month. Just be careful of that.
Thanks for another great video, Tim. I'd just like to point out that the mongo driver is quite clever and can do lambda expressions within Find() and other methods. So for example things like this are perfectly possible: return await db.Find(x => x.UserId == userId) .Skip(page * pagesize) .Limit(pagesize) .ToListAsync();
Thanks Tim. I'm fresh out of a programming qualification and often used your videos to further understand things, and now am currently building my first free lance production application. The application has a requirement for a database so this this video appearing is very timely. My first prototype used SQL but it felt like overkill for the data structure. I have heard a lot about Mongo from current web stacks (MEAN, MERN etc) but didn't realise I could use it from C#. I look forward to trying this out instead and seeing how it goes for my application.
Even though I don't like how long the videos are, they're on a whole other level of usefulness compared to any other online tutorial. You're a godsend, Tim
Wow awesome video as always Tim! Since you asked at the end of your video, perhaps you can make a subsequent video explaining / integrating Microsoft Identity with MongoDB!
Excellent Tutorial ! I like the way you create these these common generic functions so we can directly use them with our models. Thank you very much for your effort and time. Stay blessed .
One of the best tutorial I have followed so far! Thank you for your time and effort for making the video. Background explanation was also clear and nice to listen to. Keep it up!
Awesome guide and so easy to follow. Thank you very much Tim! I would love to see maybe simple CRUD web app using net core api with MongoDB. That would be really awesome. I am hard core beginner in C# and this was so easy to follow.
Need to decorate the NameModel a bit in order to get it to work now. 1:01:19 [BsonIgnoreExtraElementsAttribute] public class NameModel { [BsonId] public Guid Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
Tim you came in clutch with this tutorial....I just started using the database so your intro will be very helpful... I'm from Jamaica so the Grammer may differ from standard English 😅
I love your videos... My English is max. intermediate but I understood everything because of your great and clean vocalization. Of course thanks for the information too...
Hi Tim thank you so much for you video's!! You are a online hero!! It would be awesome if you could make video where you integrate .Net Core Identity to a MongoDB back end (like in this video). Thank you so much!!
42:55 - if it is db.GetCollection(table) does it mean even though you want to get one single record (by id) does it load whole collection ? Does it fetch the whole table even if I want 1 record from db ?
Very Good One. Thanks. Big questions are Group, Index, Search Speed On non-Id columns, and datagridview datasource with all these not consistent documents.
Hi Tim, awesome video again. I really like your channel. This was the first time I come across with MongoDB. Do you have or plan another videos about it? I'm planning to create an application and MongoDB look promising. Thanks, Jakub
I do plan on using it in other videos. It will also be one of the database types in my Foundation in C# module 8 course on data access. That comes out this month.
I have not gone through the whole video yet but thanks alot. I did not get sql server to install correctly but this works and will get me further along now :)
Are there equivalents in MongoDB (or NoSQLs in general) to SQL views, stored procedures and triggers? Does it even make any sense to think in these terms when it comes to NoSQL?
As far as views and stored procedures, no not really. There doesn't need to be because we treat data differently. We don't focus on relationships, we focus on complete documents. We approach the problem differently, so we end up using different solutions. There can be triggers in a NoSQL database. For instance, an Azure Function can be triggered from a data change event in a CosmosDB database.
Hi pls, fix me if I wrong. The query "LadAllRecords" occasionally worked for NameModel collection, just because the table didn't contain documents with Address or DOB at this moment. Probably you forgot to decorate class NameModel with [BsonIgnoreExtraElements]?
Hello, I started to develop my game like a year and 3 months ago, I am alone doing this, no easy feat let me tell you and I am using Unity. I planned and divided my project in phases, first I would develop the base of the game as single player, then later on I would use the asset Fishnetworking and convert my single player game into a multiplayer. And after I finished converting my systems I hit to a point where I would need a database. For example I have features such as character creation, also I want to make a guild system and a group system. Besides of the other systems, as they need a database as well to save data, so I started to look and found about mongoDB, at first I wanted to go for mySQL, but I saw that NoSQL might be a better fit for my game. And so I started, but to me it's kind overwhelming, because I am just starting on the database field. And thank goodness I found your video, it goes to the point and you actually created a helper class to kind interact when I want to manage my database in some way. I am so thankful for that, this video helped me alot :)
Hey Tim Corey, really great video here! I had a few questions: First, how would we get a Guid in a real world situation? It looks like they are user specific, and in your video you grabbed it from the console if I remember correctly. @46:05 Is Guid needed for security? Could we just find a user based on UserName? Thanks for your video,
I am using the developer tools to inspect the objects I have in memory (3 PersonModel objects). Inside of those objects are the GUIDs. So, if you wanted to access that data, you just pull it out of the object (or use the object). No need to involve the developer tools. I was just showing you the values instead of printing them to the Console. As for needing the GUID, you use that for easy lookup of the record. You can query by other values, but the GUID will be unique and is indexed.
I will add it to the list. Thanks for the suggestion.
5 лет назад+1
Thank you for this tutorial. It has enlightened me. What if there is more then two tables and there are some relations between them? For example a trainings table and an employees and a table that handles for attendees. I think this is for relational databases. Could it even build with mongo db?
Yes it can be built but more likely you would set it up differently. If an employee attends a training session, that training session would be attached to the employee record. No relationship needed.
Or you can do it the normalized way and add a GUID of some record from another collection, just like a foreign key. But then you have to handle fetching related data manually, Mongo won't do that for you. Altough this is a valid method, once the scheme gets more complex, it can very well turn into a huge mess.
hey I just watched the video and it was very helpful especially because there is rarely any approachable materials about mongodb with C# it could be amazing to have a playlist tutorial app with mongodb
so know this is an old view, but I'm wondering for a structure standpoint it seems that alot of the "child" tables or reference tables that have many to many relations are just consumed by the parent object /document..... is there any case where two document need to be related or do just call the data from document 1 that you need and then add to document 2 for future reads? I hope that was clear but i proable wasn't I contune watching your suggestion app and hope my questions get answered :)
UpdateOptions was replaced by ReplaceOptions. This is my modified implementation: public void UpsertRecord(string table, Guid id, T reccord) { var collection = db.GetCollection(table); var result = collection.ReplaceOne( new BsonDocument("_id", new BsonBinaryData(id, GuidRepresentation.Standard)), reccord, new ReplaceOptions { IsUpsert = true }); }
1:01:53 Hi, Tim! When I tried to load records into List, I got the following error "'Element 'dob' does not match any field or property of class MongoDBDemo.NameModel.'". So, for me adding just the "id" field did not solve the problem and the system looked for all fields coming from the database to be mapped. I determined that the exception was triggered by ToList() call. Any idea why it behaved differently in my environment? [Edit] Just figured it out. Needed to decorate NameModel with [BsonIgnoreExtraElements]. Still interesting that it worked for you. Perhaps the Mongo Driver NuGet package version - I used 2.9.2.
Thank you very much. At the end, you mentioned when to use MongoDB and you said it has this issue of transactions, would it be suitable to use MongoDB in an online shop application?
Definitely. MongoDB can be used (at this point) anywhere that a relational database (like SQL) can be used. It is different and you have to approach things differently, but MongoDB is fully ACID compliant.
In the Suggestion Site course (playlist here on RUclips), we did transactions when we handled Upvoting suggestions. I don't know the exact video, though.
new BsonDocument("_id", id) BsonValue.implicit operator BsonValue(Guid)' is obsolete: 'Use the BsonBinaryData constructor instead and specify a Guid representation.
Following this advice, the method becomes the following and compiles without a warning. public void UpsertRecord(string table, Guid id, T record) { var collection = GetCollection(table); var filter = new BsonDocument("_id", new BsonBinaryData(id, GuidRepresentation.Standard)); collection.ReplaceOne(filter, record, new ReplaceOptions { IsUpsert = true }); }
Thanks for the very useful tutorial Tim! However, i am struggling to retrieve objects who are child of one array object. So i have one object which is an array, in this array are multiple nested objects of different classtypes... Do you have an idea how to find the right 'nested objects?
I have the same issue, I would like to just update a child (Object) of an document, because loading all the children from the document seems a little bit inefficient. The thing is that I can't access the child by using its id, it seems like mongodb tries to search in the parents but doesn't search in the children. Is there a way to search ID in children and update it or search the parent by ID and then update ONLY a certain child?
Awesome! Hey, I'm speaking at Philly Code Camp in October ( phillydotnet.org/2019-2.html ) and TechBash in November ( techbash.com/ ). Both are fairly close to us. Stop by and say hi if you go to either event. I would love to meet you.
This is the best tutorial I see for SQL Relational programmers to an introduction to a Non-SQL database. I am getting a timeout, I do not see on the tutorial where you tell the C# the connection. Is this automatically? How do I check that?
If you are getting a timeout, it probably means your connection string is incorrect. I would check that and verify all of the details are correct for your machine.
You actually don't need to include the Id field in the NameModel if you add the BsonIgnoreExtraElements attribute to the NameModel class. You got the error about not having an Id because that attribute was not on the class and all of your records had an Id field. You don't get an error about the PrimaryAddress because none of your records had that.
Hi Tim :). Great video like always! I have two questions: 1. Every time you do - "var collection = db.GetCollection(table);" - do you bring all the data in the Users collection into the local memory? 2. Is there a better way today to bring data by id (or another field)? all the - "var filter = Builders.Filter.Eq("Id", id);" and then use it like - "collection.Find(filter).First();" looks so clumsy and not elegant... Thanks man!
No, getting the collection is like opening a connection to that set of documents (that "table"). As for a better way, not really. You create the better way by wrapping that in a method.
Awesome! Learn, learn, learn. Thanks for all the content! Question: What do you think about the Factory pattern where the concrete classes connect to MongoDb or SQL Server. Would this be non-nonsensical since they are so different?
Hi Tim, I am facing an issue while referring to your MongoDb tutorial, which is from 1:01:10 (create NameModel), I have an existing record that contains my Details object (map with DetailsModel class) in my MongoDb. However, when I just wanted to retrieve Id, FirstName and LastName (referred to your NameModel) in my C# program, it also throws System.FormatException as well. Anyhow, your tutorial video still awesome as always!
@@IAmTimCorey My Id property in NameModel class is Guid which is same as yours. Lets say i got PrimaryAddress object which is same as your PersonModel. And I have NameModel which I only wanted to retrieve Id, FirstName and LastName. However when I wanted to map into my NameModel, the system throws exception: 'Element 'PrimaryAddress' does not match any field or property of class MongoDbDemo.Program+NameModel.'
Hi Tim, thanks for the video, really useful ! I want to log data from a sensor, I wonder if it is better for me to create one document pro session, that contains a list of data that I update everytime I want to add a value, of if I should juste create one collection for one set of measures, and so that every measure + timestamp would be a document. I asked Chat GGT to see, according to it, it would be better to create one document for every single measure (measure + timestamp). Thanks for your help !
You could make a case for either, but I do think that the most common way to do that would be one document with multiple readings. You are keeping like things together in one object.
@@IAmTimCorey Thank you for your reply! Indeed it would be easier to get back the values to display them into a chart. My fear was the update function would rewirte the whole List when seeing that the size of the List is different. Anyway, let's try it!
Hi Tim, I have a question to this, how do people hide passwords if they use Atlas for example and the app i avaiable on the internet, people could just refacot it and see the connection string with password. Encryption within the code is also not working since you can just refactor that too. Is there any best practice for that? How do other people safe their data in cloud databases without showing any connectionstring/password in code?
If you are running a web app, the connection string will be securely on the server. If you are running a desktop application, that's a problem that cannot be solved through encryption, etc. as you pointed out. The solution is to go through an API like I do in the TimCo Retail Manager series. More Info: ruclips.net/video/rFncI9yfY-E/видео.html
@@IAmTimCorey hey thanks for your answer, after I asked I checked pretty much all your Videos. Im half trough the TimCo Retail Manager series Playlist. Thanks for your content, you are doing gods work here ;)
I see you use [BsonId] for "tagging" the Id so Bson can notice that it is in fact the id. I tried not using it and still worked so, is it strictly necessary or if I don't use may the data base break at some point?
The default value of Guid in C# is not null, it is empty Guid. Now are you saying that MongoDb checks empty Guid for insert and non empty Guid for update? @24:45
Hi Tim, do you have a video regarding singleton usage in C#? If I were to build a wpf/winform app that uses mongodb, it would make sense to use a singleton for the MongoCRUD class right? That way I can easily use it across all my winform classes?
The easiest way to utilize a Singleton is to use Dependency Injection. I demo that in a few places, including the TimCo Retail Manager series. In this case, though, I'm not sure that a singleton is the right choice since it stores database information upon instantiation. If you only have one DB then maybe.
Video is uploaded 5 minutes ago and someone already clicked a dislike. Hey man, you even did not watch this!!!
Thank you Tim for this tutorial. Thumb up!
Lol, dident even remake the dislike, there are some bad people runing round the internet :)))
Yeah, I saw that. The funny thing is that RUclips cares about engagement. The more engagement a video gets (likes, dislikes, comments), the higher they rank it so technically, that person is helping me just a bit. :-)
Yes, very poor show to dislike without even viewing. Thank you Tim for giving us your time and knowledge.
She must be an Ex lol
Awesome introduction to NoSQL for .NET developers! Thanks as always and best wishes Tim.
Thank you!
Dude, your videos are great. Neither rushed nor slow, and including all of the salient information a newbie to a subject needs. Kudos!
Thank you!
Thank you for a good introduction, Tim. Been using MSSQL for many years and knows it very well. For me this was a first look at NoSQL, and it looks very very interesting! At first it feels very uncomfortable with the loose data structure but it really makes a lot of sense. Just the fact that the termonology is "documents" rather than "tables" makes so much sense; love that we're simply just storing the data as it actually is instead of joining tables and all that.
Glad it was helpful!
Hello Tim,
You have a logical error in this video starting from the time position 1:00:00, when you tried to bring and use new NameModel class. It is working for you because you deleted the last record that has PrimaryAddress just before you introduced NameModel. If you actually have couple existing records in DB, some of them have only FirstName and LastName (and Id for sure) and some have FirstName, LastName and PrimaryAddress, you won't be able to use NameModel as you just did. You will have unhandled exception in the line:
return collection.Find(new BsonDocument()).ToList();
saying: 'Element 'PrimaryAddress' does not match any field or property of class MongoDBDemo.NameModel.'
So you cannot "narrow down" your objects so easily if you actually have additional data in the MongoDB, you will have to use some conversion.
I forgot to show that. Sorry about that. The solution is to put a [BsonIgnoreExtraElements] on the top of your model and it will work. I meant to show it. Thanks for pointing it out.
Thank you Tim Corey, you explained that mongoDb is faster than sql perfectly. Have learned something new!
It can be faster. That doesn't mean it is always the better choice. There are always tradeoffs. I'm glad it was valuable.
0:00 - Intro
1:27 - Setup: Mongo DB Community Server installation
8:06 - Creating Demo Core Console App
9:35 - MongoDB NuGet
11:04 - Mongo CRUD
12:50 - "Connection string"
14:50 - Connecting to database
17:13 - Data storage in MongoDB
24:39 - Inserting data in database
28:07 - Changing data set structure
30:45 - Inserting different data set in the table and benefit of NoSQL
36:52 - Retrieving data form database
42:00 - MongoDB Query
48:40 - Update and delete methods
54:28 - Insert or Update record
59:00 - Delete record
59:42 - Returning different data model from database record
1:02:55 - Demo code clean up
1:03:20 - MongoDB when and why?
1:15:15 - Summary and concluding remarks
Thank you!
Mr. Corey, thank you. I am accepted to an internship position in an international company called Exadel. The knowledge you are sharing in RUclips is priceless. I am still learning from your content and plan to one your paid courses once I receive my salary as a .net developer.
Your student from Uzbekistan :)
Awesome! I am glad I was able to be a part of your journey.
UpsertRecord with the ReplaceOne, 52:39, is obsolete in new Mongo installs. Need to replace "new BsonDocument..." with a filter on the ID
public void UpsertRecord(string table, Guid id, T record)
{
// Create the collection
var collection = db.GetCollection(table);
// Create the filter
var filter = Builders.Filter.Eq("Id", id);
// Replace the record via an upsert
// Upsert is an update if the filter finds something or
// an insert if there is nothing matching
collection.ReplaceOne(
filter,
record,
new ReplaceOptions { IsUpsert = true });
}
Thanks for sharing.
Thanks a lot.
Tim Corey Very useful tutorial. Learned all the basics for a NoSql database in just 1 hour. Keep up the good work!
Glad it was helpful!
I am learning NoSQL for the first time and its the best video have found it very informative and in very understandable tone. MongoDB is very good NoSQL option with .NET Core. Thanks
Glad it was helpful!
Another terrifically educational video. Thanks for taking the time to produce and post. Always look forward to your content.
You are welcome.
Thanks for this. It really helped me understand MongoDB and when to use it. I've been using MSSQL for 20 years. I don't see using mongodb for anything at work yet, but I'm testing it with a app at home.
Glad it was helpful!
Your video is NEVER too long!
Thanks!
This is a great Video, I've done a introductory course in MongoDB, but as you said it right "this is my new tool... then now what?" even they in that course just tell you about using it with console and Atlas so this is a very good piece of material to integrate it with C# Thanks a lot.
Glad it was helpful!
Thanks, Tim that is one of the most beautiful no SQL videos on youtube.
Thank you!
Thanks Tim. Very nice introduction to MongoDB and NoSQL world.
Glad it was helpful!
As usual, a very, very nice video. Got a project on this noSQL thing and had no idea where to start. Was most-delighted when I saw you did a video because I knew it was going to be the best place to start. Took me through all the CRUD operations and can't believe how simple it is in C#. The Cosmo Db in Azure is a "fake" MongoDb. It offers various "API"s, one of them is Mongo. So, for the most part, you send it normal Mongo instructions and it responds, just like Mongo. Cool. And, only costs about $5-$6 a month to get started. Connection String inserts just like you said it would in your video. I am a SQL Server bigot but am most-happy about being able to add this to my resume and quiver. Yeah, I can see the positives. Did have 2 questions. One, more complex filter usage for query, upsert and delete. Two, can you remove a property in a "document" once added. Not the value of the property but the property itself. Say you added something and company policy has changed and that property, from each record ("document") has to be removed for legal reasons. Also, I'm curious, it's so fast? There has to be indexing then. Is it/are they clustered or non-clustered? Would be very surprised if there were no indexes. I found the constant reference to Models interesting. So non-structured and yet the model usage. Not sure how to wrap my head around that but, coming from a very-structured world, I find this really easy going. Thanks again Tim. All the best. Awesome.
I will be doing other videos shortly using MongoDB. As for removing the property, you should be able to pull down the record and then save it without the property. As for the speed, its speed comes primarily from the fact that all of the data is together instead of pulling from separate locations. One quick note: I originally thought you could drop the units to 100 for MongoDB but you cannot so the smallest database you can get is 400 units, which costs $25/month. Just be careful of that.
Thanks for another great video, Tim. I'd just like to point out that the mongo driver is quite clever and can do lambda expressions within Find() and other methods. So for example things like this are perfectly possible:
return await db.Find(x => x.UserId == userId)
.Skip(page * pagesize)
.Limit(pagesize)
.ToListAsync();
Awesome! Thanks for sharing.
"That's the basic structure of MongoDB meaning that isn't really a structure" you made my day :D
I'm glad you enjoyed it.
Great video Tim! And a nice, simple way of handling CRUD operations without requiring models to inherit from a common 'entity' base class.
Thanks. I considered a common model base but decided against it for this video.
For all free tutorial in youtube, this is great
Thank you!
Thanks Tim. I'm fresh out of a programming qualification and often used your videos to further understand things, and now am currently building my first free lance production application. The application has a requirement for a database so this this video appearing is very timely. My first prototype used SQL but it felt like overkill for the data structure. I have heard a lot about Mongo from current web stacks (MEAN, MERN etc) but didn't realise I could use it from C#. I look forward to trying this out instead and seeing how it goes for my application.
Excellent. Let us know how it goes.
Tq for your short and sweet video about how to use MongoDB in C#, it was handy.
You are most welcome. Thanks for watching.
Even though I don't like how long the videos are, they're on a whole other level of usefulness compared to any other online tutorial. You're a godsend, Tim
I am glad you find them valuable.
Wow awesome video as always Tim! Since you asked at the end of your video, perhaps you can make a subsequent video explaining / integrating Microsoft Identity with MongoDB!
We appreciate the suggestion and I have added it to Tim's list.
Your explations are best Tim just thank you.
You are welcome.
One of the best tutorial found online. Thank you so much
Your endorsement is much appreciated! Thank you for helping to spread the word.
Awesome video explaining how u use MongoDB with C# , thanks so much 💙💙
You're very welcome!
Excellent Tutorial ! I like the way you create these these common generic functions so we can directly use them with our models. Thank you very much for your effort and time. Stay blessed .
You're very welcome!
One of the best tutorial I have followed so far! Thank you for your time and effort for making the video. Background explanation was also clear and nice to listen to. Keep it up!
You're very welcome!
Awesome guide and so easy to follow. Thank you very much Tim!
I would love to see maybe simple CRUD web app using net core api with MongoDB. That would be really awesome.
I am hard core beginner in C# and this was so easy to follow.
I'm glad it was so easy to follow. I'll add your suggestion to the list.
Need to decorate the NameModel a bit in order to get it to work now. 1:01:19
[BsonIgnoreExtraElementsAttribute]
public class NameModel
{
[BsonId]
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Thanks for sharing.
Tim you came in clutch with this tutorial....I just started using the database so your intro will be very helpful... I'm from Jamaica so the Grammer may differ from standard English 😅
Excellent!
I love your videos... My English is max. intermediate but I understood everything because of your great and clean vocalization. Of course thanks for the information too...
Awesome!
Great tutorial! NoSQL database can be a great choice for the new projects!
Thanks!
Top one best video i have seen about mongodb c#. Thanks alot for share!
His goal is to make learning easier. It appears he is doing just that!
Hi Tim thank you so much for you video's!! You are a online hero!!
It would be awesome if you could make video where you integrate .Net Core Identity to a MongoDB back end (like in this video). Thank you so much!!
I will add it to the list. Thanks for the suggestion.
Mark my words: Tim is the best!
Thanks!
Thank you Tim that was very informative
You are welcome.
42:55 - if it is db.GetCollection(table) does it mean even though you want to get one single record (by id) does it load whole collection ? Does it fetch the whole table even if I want 1 record from db ?
thank you for explaining it so clearly and calmly
Glad it was helpful!
Great Video. Please do advance Level video also which covers Aggregation, ACID AND Triggers.
We appreciate the suggestion and I have added it to Tim's list.
Very Good One. Thanks. Big questions are Group, Index, Search Speed On non-Id columns, and datagridview datasource with all these not consistent documents.
Thanks for the suggestions.
Hi Tim, awesome video again. I really like your channel. This was the first time I come across with MongoDB. Do you have or plan another videos about it? I'm planning to create an application and MongoDB look promising. Thanks, Jakub
I do plan on using it in other videos. It will also be one of the database types in my Foundation in C# module 8 course on data access. That comes out this month.
Thank you Tim. You are best.
You are very welcome
I have not gone through the whole video yet but thanks alot. I did not get sql server to install correctly but this works and will get me further along now :)
You are welcome.
Are there equivalents in MongoDB (or NoSQLs in general) to SQL views, stored procedures and triggers? Does it even make any sense to think in these terms when it comes to NoSQL?
As far as views and stored procedures, no not really. There doesn't need to be because we treat data differently. We don't focus on relationships, we focus on complete documents. We approach the problem differently, so we end up using different solutions. There can be triggers in a NoSQL database. For instance, an Azure Function can be triggered from a data change event in a CosmosDB database.
@@IAmTimCorey Thank you!
great tutorial. I first had some trouble deserializing because of my MongoD release (3.6.5). With version 4 works fine
Glad you got it figured out.
Great video content as always. Thanks for sharing the knowledge, it keeps me motivated to learn more.
You are welcome.
Hi pls, fix me if I wrong.
The query "LadAllRecords" occasionally worked for NameModel collection, just because the table didn't contain documents with Address or DOB at this moment.
Probably you forgot to decorate class NameModel with [BsonIgnoreExtraElements]?
Yep, that will work.
Thank you! Just what I was looking for.
Eye-opening! Thank you, Tim!
You are welcome.
Awesome video. You really make programming simple.
Thank you!
It was very helpful to me.
Thank you Tim.
You are welcome.
Love the way you explain. Good stuff there!
Thanks!
Hello, I started to develop my game like a year and 3 months ago, I am alone doing this, no easy feat let me tell you and I am using Unity. I planned and divided my project in phases, first I would develop the base of the game as single player, then later on I would use the asset Fishnetworking and convert my single player game into a multiplayer. And after I finished converting my systems I hit to a point where I would need a database. For example I have features such as character creation, also I want to make a guild system and a group system. Besides of the other systems, as they need a database as well to save data, so I started to look and found about mongoDB, at first I wanted to go for mySQL, but I saw that NoSQL might be a better fit for my game. And so I started, but to me it's kind overwhelming, because I am just starting on the database field.
And thank goodness I found your video, it goes to the point and you actually created a helper class to kind interact when I want to manage my database in some way. I am so thankful for that, this video helped me alot :)
Hey Tim Corey, really great video here!
I had a few questions:
First, how would we get a Guid in a real world situation? It looks like they are user specific, and in your video you grabbed it from the console if I remember correctly. @46:05
Is Guid needed for security? Could we just find a user based on UserName?
Thanks for your video,
I am using the developer tools to inspect the objects I have in memory (3 PersonModel objects). Inside of those objects are the GUIDs. So, if you wanted to access that data, you just pull it out of the object (or use the object). No need to involve the developer tools. I was just showing you the values instead of printing them to the Console.
As for needing the GUID, you use that for easy lookup of the record. You can query by other values, but the GUID will be unique and is indexed.
@@IAmTimCorey Great answer. Thanks Tim
It is nice video Tim. Thank you.
You are welcome.
Thanks for the information. Really liked the video.
You are welcome.
What a wonderful tutorial!
Thanks!
That is what i am lookin for. Thx for sharing with us
You're welcome 😊
Great intro! just cleared my mind!
Excellent!
Great thanks, a value able content and much appreciated instructions in the end of the video.
You are most welcome. Thanks for watching.
Great tutorial as always. Tutorial how and when to use relations in MangoDB may be helpful too. Thanks a lot.
I will add it to the list. Thanks for the suggestion.
Thank you for this tutorial. It has enlightened me.
What if there is more then two tables and there are some relations between them? For example a trainings table and an employees and a table that handles for attendees. I think this is for relational databases. Could it even build with mongo db?
Yes it can be built but more likely you would set it up differently. If an employee attends a training session, that training session would be attached to the employee record. No relationship needed.
Or you can do it the normalized way and add a GUID of some record from another collection, just like a foreign key. But then you have to handle fetching related data manually, Mongo won't do that for you. Altough this is a valid method, once the scheme gets more complex, it can very well turn into a huge mess.
Thanks just got an assignment of school about mongo
You are welcome.
hey I just watched the video and it was very helpful especially because there is rarely any approachable materials about mongodb with C#
it could be amazing to have a playlist tutorial app with mongodb
I will add it to the list. Thanks for the suggestion.
so know this is an old view, but I'm wondering for a structure standpoint it seems that alot of the "child" tables or reference tables that have many to many relations are just consumed by the parent object /document..... is there any case where two document need to be related or do just call the data from document 1 that you need and then add to document 2 for future reads? I hope that was clear but i proable wasn't I contune watching your suggestion app and hope my questions get answered :)
Hi Tim, It appears the ReplaceOne has been made obsolete. What the is new way of doing the same thing for an Upsert?
UpdateOptions was replaced by ReplaceOptions. This is my modified implementation:
public void UpsertRecord(string table, Guid id, T reccord)
{
var collection = db.GetCollection(table);
var result = collection.ReplaceOne(
new BsonDocument("_id", new BsonBinaryData(id, GuidRepresentation.Standard)),
reccord,
new ReplaceOptions { IsUpsert = true });
}
1:01:53 Hi, Tim! When I tried to load records into List, I got the following error "'Element 'dob' does not match any field or property of class MongoDBDemo.NameModel.'". So, for me adding just the "id" field did not solve the problem and the system looked for all fields coming from the database to be mapped. I determined that the exception was triggered by ToList() call. Any idea why it behaved differently in my environment?
[Edit] Just figured it out. Needed to decorate NameModel with [BsonIgnoreExtraElements]. Still interesting that it worked for you. Perhaps the Mongo Driver NuGet package version - I used 2.9.2.
That's interesting. Not sure why you had that error.
Thank you very much. At the end, you mentioned when to use MongoDB and you said it has this issue of transactions, would it be suitable to use MongoDB in an online shop application?
Definitely. MongoDB can be used (at this point) anywhere that a relational database (like SQL) can be used. It is different and you have to approach things differently, but MongoDB is fully ACID compliant.
Very helpful tutorial to start MongoDB with .Net. Can you please make some more tutorials to discuss more details about it?
I will add it to the list. Thanks for the suggestion.
Hello Tim, the video was great! Do you have a video explaining how to implement a transactions in mongo DB?
In the Suggestion Site course (playlist here on RUclips), we did transactions when we handled Upvoting suggestions. I don't know the exact video, though.
Thanks for the video, very easy to follow along :)
You are welcome.
The compiler now reports a warning with the line
new BsonDocument("_id", id)
BsonValue.implicit operator BsonValue(Guid)' is obsolete: 'Use the BsonBinaryData constructor instead and specify a Guid representation.
Following this advice, the method becomes the following and compiles without a warning.
public void UpsertRecord(string table, Guid id, T record)
{
var collection = GetCollection(table);
var filter = new BsonDocument("_id", new BsonBinaryData(id, GuidRepresentation.Standard));
collection.ReplaceOne(filter, record, new ReplaceOptions { IsUpsert = true });
}
Thanks for sharing.
Thanks for the very useful tutorial Tim!
However, i am struggling to retrieve objects who are child of one array object. So i have one object which is an array, in this array are multiple nested objects of different classtypes...
Do you have an idea how to find the right 'nested objects?
I am not sure I see what the problem is. If you load an object, you will load all of the nested objects inside of it.
I have the same issue, I would like to just update a child (Object) of an document, because loading all the children from the document seems a little bit inefficient. The thing is that I can't access the child by using its id, it seems like mongodb tries to search in the parents but doesn't search in the children. Is there a way to search ID in children and update it or search the parent by ID and then update ONLY a certain child?
It was great training, thanks Tim
You are welcome.
This was really helpful bro. thank you so much
Glad it helped!
Hey Tim, amazing videos. I live near Scranton PA. 😀
Awesome! Hey, I'm speaking at Philly Code Camp in October ( phillydotnet.org/2019-2.html ) and TechBash in November ( techbash.com/ ). Both are fairly close to us. Stop by and say hi if you go to either event. I would love to meet you.
IAmTimCorey sure definitely, I actually work in Conshohocken as .Net developer.
This is the best tutorial I see for SQL Relational programmers to an introduction to a Non-SQL database. I am getting a timeout, I do not see on the tutorial where you tell the C# the connection. Is this automatically? How do I check that?
If you are getting a timeout, it probably means your connection string is incorrect. I would check that and verify all of the details are correct for your machine.
You actually don't need to include the Id field in the NameModel if you add the BsonIgnoreExtraElements attribute to the NameModel class. You got the error about not having an Id because that attribute was not on the class and all of your records had an Id field. You don't get an error about the PrimaryAddress because none of your records had that.
Thanks for sharing.
Tim, thank you so much for making this videos. You have made me a better developer. Does MongoDB have an equivalent of a SQL stored procedure?
No, but there isn't a need for one.
Nice vid, more mongo pls!!!
Thanks. More will be coming in the future.
Hi Tim :).
Great video like always!
I have two questions:
1. Every time you do - "var collection = db.GetCollection(table);" - do you bring all the data in the Users collection into the local memory?
2. Is there a better way today to bring data by id (or another field)?
all the -
"var filter = Builders.Filter.Eq("Id", id);"
and then use it like -
"collection.Find(filter).First();"
looks so clumsy and not elegant...
Thanks man!
No, getting the collection is like opening a connection to that set of documents (that "table"). As for a better way, not really. You create the better way by wrapping that in a method.
Awesome! Learn, learn, learn. Thanks for all the content! Question: What do you think about the Factory pattern where the concrete classes connect to MongoDb or SQL Server. Would this be non-nonsensical since they are so different?
It is hard to switch from a SQL database to a NoSQL database. The types are just so different.
Thank you so much! Loved this!
You are welcome.
Thank you Tim
You are welcome.
Hi Tim, I am facing an issue while referring to your MongoDb tutorial, which is from 1:01:10 (create NameModel), I have an existing record that contains my Details object (map with DetailsModel class) in my MongoDb. However, when I just wanted to retrieve Id, FirstName and LastName (referred to your NameModel) in my C# program, it also throws System.FormatException as well. Anyhow, your tutorial video still awesome as always!
My guess is that you are trying to map a type (string, int, etc.) to the wrong type in your model (so mapping a GUID to an int property).
@@IAmTimCorey My Id property in NameModel class is Guid which is same as yours. Lets say i got PrimaryAddress object which is same as your PersonModel. And I have NameModel which I only wanted to retrieve Id, FirstName and LastName. However when I wanted to map into my NameModel, the system throws exception: 'Element 'PrimaryAddress' does not match any field or property of class MongoDbDemo.Program+NameModel.'
@@IAmTimCorey I have solved by adding [BsonIgnoreExtraElements] :-)
@@yukiobest6004 I just had the same issue, glad you found about the [BsonIgnoreExtraElements]. just used it on the class and it worked.
@@yukiobest6004 Thank you so much! I also have that issue 😂
very very helpfull tutorial, perfect explain about mongodb database. thank u master
Glad it was helpful!
Hi Tim, thanks for the video, really useful ! I want to log data from a sensor, I wonder if it is better for me to create one document pro session, that contains a list of data that I update everytime I want to add a value, of if I should juste create one collection for one set of measures, and so that every measure + timestamp would be a document. I asked Chat GGT to see, according to it, it would be better to create one document for every single measure (measure + timestamp). Thanks for your help !
You could make a case for either, but I do think that the most common way to do that would be one document with multiple readings. You are keeping like things together in one object.
@@IAmTimCorey Thank you for your reply! Indeed it would be easier to get back the values to display them into a chart. My fear was the update function would rewirte the whole List when seeing that the size of the List is different. Anyway, let's try it!
if someone else want to implement timeseries into mongodb, look at www.mongodb.com/blog/post/building-with-patterns-the-bucket-pattern
One other advantage you missed was scaling is super simple in noSql Dbs
That is a big benefit.
wonderful tutorial!
Thank you for sharing!
Hi Tim, I have a question to this, how do people hide passwords if they use Atlas for example and the app i avaiable on the internet, people could just refacot it and see the connection string with password. Encryption within the code is also not working since you can just refactor that too. Is there any best practice for that? How do other people safe their data in cloud databases without showing any connectionstring/password in code?
If you are running a web app, the connection string will be securely on the server. If you are running a desktop application, that's a problem that cannot be solved through encryption, etc. as you pointed out. The solution is to go through an API like I do in the TimCo Retail Manager series.
More Info: ruclips.net/video/rFncI9yfY-E/видео.html
@@IAmTimCorey hey thanks for your answer, after I asked I checked pretty much all your Videos. Im half trough the TimCo Retail Manager series Playlist. Thanks for your content, you are doing gods work here ;)
I see you use [BsonId] for "tagging" the Id so Bson can notice that it is in fact the id. I tried not using it and still worked so, is it strictly necessary or if I don't use may the data base break at some point?
I believe if you use Id then MongoDB assumes. Tagging it makes it clear.
If possible do a video on ACID - Comparison using SQL SERVER and MongoDB using an example.
I will add it to the list. Thanks for the suggestion.
The default value of Guid in C# is not null, it is empty Guid. Now are you saying that MongoDb checks empty Guid for insert and non empty Guid for update? @24:45
Correct. It checks and identifies that it needs a GUID and creates one.
Got a question on how to do logs using Mongodb. Can you do a tutorial on how to do log with Mongodb?
You can use it like any other database. I'll see what I can do about showcasing that.
There's a Nuget for Log4Net using MongoDb, but it hasn't been updated for .Net Core or .Net Standard.
Great explanation in simple way
Thanks!
Hi Tim, do you have a video regarding singleton usage in C#? If I were to build a wpf/winform app that uses mongodb, it would make sense to use a singleton for the MongoCRUD class right? That way I can easily use it across all my winform classes?
The easiest way to utilize a Singleton is to use Dependency Injection. I demo that in a few places, including the TimCo Retail Manager series. In this case, though, I'm not sure that a singleton is the right choice since it stores database information upon instantiation. If you only have one DB then maybe.
Thanks a lot, great tutorial :)
You're welcome!