First steps with MongoDB (in C#)
Modern problems require modern solutions. One of these modern solutions is MongoDB, a NoSQL database system.
I was sceptical when I first heard about MongoDB… No tables, no schemas… I thought there is no structure, absolute chaos. But after some tests and scenarios, I have changed my mind about MongoDB.
MongoDB can have an enormous influence on object-oriented-programming (OOP). It brings simplicity in the world of storing data in databases, cause most of the time you have already all schemas you need, to store your objects. Your written code is your schema.
With MongoDB you will have an enormous time saving, cause you don´t have to care about database schemes or table structure, no more complicated statements inside string variables. Sounds nice or?
You can write your code as you need it and store your objects exactly in this way, object in — object out, it´s easy, is it? But what if you need to search an object inside your database? No problem, with the MongoDB Driver you can use LINQ to find your objects, so you don´t need to think about your database, your database creates itself when you write your code and this is the biggest feature which MongoDB brings to you, Time!
How to get started?
Photo by matthew Feeney on Unsplash
First of all you just need to add the references to MongoDB.Driver and to start with your MongoDB tests, you will need a MongoDB database.
You can create a MongoDB database with MongoDB-Atlas so you don´t need to install MongoDB on your development computer. But if you want you can also create a local cluster, with the community server, to get started with MongoDB.
When you are ready and you have your MongoDB cluster, you can begin to connect to your database, in your local environment there is no-security configuration so you can connect without any credentials.
Your connection string is then: mongodb://localhost:27017
If you just want to look inside with the MongoDB Compass which comes with the installation you can copy and paste the connection string into MongoDB Compass.
MongoClient client = new MongoClient("mongodb://localhost:27017");
As you can see in the above code, to connect to your database you must just setup your client with your connectionstring. In case you have created a user you connectionstring looks like this: mongodb://yourUser:yourPassword@localhost:27017
After you initialized your client you can get (and if not exist, create) your new database and get (or create if not exist) your first collection.
MongoClient client = new MongoClient("mongodb://localhost:27017");
IMongoDatabase database = client.GetDatabase("myTestDatabase");
IMongoCollection<MyTestObject> myTestObjectCollection = database.GetCollection<MyTestObject>("myTestObjectCollection");
Now where you have your collection with a sample object (in my case MyTestObject), you can start interacting with your collection.
Inserting
To insert data in your collection you can do the following:
MyTestObject obj = new MyTestObject(){
// You must have imported MongoDB.Bson to have access to this class,
// with this call you create a new ID as an identifier in your collection
Id = ObjectId.GenerateNewId(),
Name = "Test",
Comment = "comment here..."
};
myTestObjectCollection.InsertOne(obj);
Searching / retrieving stored objects
As I mentioned above you can search for objects with LINQ
List<MyTestObject> testsMyTestObjectEntries = myTestObjectCollection.Find(testObject => testObject.Name == "Test").ToList();
Removing objects
When you came into a situation, where you have to delete an object from database, you can run the following:
string id = "5e747750f8ca006d93762528";
myTestObjectCollection.DeleteOne(o => o.Id == ObjectId.Parse(id));
Summarized, I can say that MongoDB is a nice solution for me to simply create applications with database behind. It helps me to develop my applications faster, flexible and stressless.
All key features mentioned above, summarized:
- build your queries inside your code, with LINQ expressions (no more string variable-statements with KeyValue-Parameters)
- your class is your structure, once you build a class with all of its properties, you can store it exactly the same way inside the MongoDB Collection
- database structure as you need it, no more complicated joins
- don´t care about field mapping, as it is already managed via the MongoDB.
At the end everyone has to decide for himself which database solution to use and here too there will be various factors to consider. MongoDB is one of many solutions. I will pay more attention to MongoDB in my future projects, but it is not a solution for everything.