Pawel Pindel
Technical Leader
MongoDB is one of the most popular representatives of the NoSQL genre of databases so most developers probably don’t need to be introduced to it. If you’ve heard of NoSQL then you’ve almost certainly heard the MongoDB name too. However, this post won’t be about explaining what this magical data storage type is. I assume that since you’re reading this article you already know that, and if you want to learn more, I’ll refer you to the extensive documentation (https://www.mongodb.com/nosql-explained).
When to start working with MongoDB?
Before you start working with MongoDB, it is worth asking yourself if you really need this Mongo or any other non-relational database type, or if it is not the result of the ubiquitous hype for similar solutions. If you answered “no” to the second question, we can now consider when it is worth using a non-relational database.- You need fast access to data.
- Time of delivery of the application to production is important to you.
- You assume that the data schema changes frequently during development.
- You need to process large amounts of data, so you expect easy horizontal scaling.
- You are uncertain about the final structure of the data, or part of the data will never be structured.
- You want to build an application based on a map – here I invite you to my article I wrote recently on my blog (https://netsharpdev.com/2021/09/09/geoindexes-in-mongodb/)
- You need distributed transactions on database in the cluster.
How to configure MongoDB in .NET?
Let’s start with the fact that we have several options. We can install MongoDB locally, download the Docker image and run, or use the free option in MongoDB Atlas. I will choose the latter option because it will also allow us to load sample data. Using MongoDB Atlas we also get 2 additional replicas at our disposal, so we can practice sharding or writing from one and reading from the other.Creating database in Atlas
- To create a database, log on tohttps://cloud.mongodb.com/.
- In the selection options, we can create the database on AWS, Azure or Google Cloud.
- After the database is created, select „Connect”.
- Add the user who will be used to connect to the database and add his IP address to the access list. (For development purposes, with frequently changing IP addresses, you can allow all addresses to connect to the database – but remember not to allow such access in production!)
- Copy the connection string and save it somewhere in notepad. It will be useful when we configure the connection in our application.
Connection Configuration
The first question that comes up when using a database in our application is: “Which nuget package to use?”. There are many solutions available in our code market, nevertheless the best approach is to choose MongoDB.Driver. It is the official client, supported by MongoDB. Using it we can be sure that the latest functionalities will be implemented. You can also find EntityFramework adaptations for MongoDB, but I don’t see a single reason to use that. MongoDB as a non-relational, document database is tailored and written perfectly to not use any ORMs. It uses JavaScript to read and write data, so any object-oriented language can be very easily used to adapt JavaScript commands. Let’s go to the configuration.- Save the connection string you copied earlier in the appsettings.json settings file. Remember to replace <username> and <password> with the correct values.
- in the Startup.cs file, register MongoClient as a singleton.