This article explains MONGO DB with .NET integration and its advantages. To use the MONGO Database in the local machine setup, it should be installed on PCs or MongoDB Atlas access needed from the MongoDB Server. To download the Mongo DB community edition, go to this site, click here, open the downloading .msi file, and install. Then, open Visual Studio and install the MongoDB driver using NuGet Package Manager or run the command below in the Package Manager Console.
Install-Package MongoDB.Driver
Advantage of MongoDB with .NET interagation
- To run on various operating systems and flexible deployment in cross-platform applications' creation are good choices for .Net Core and MONGO Database.
- A seamless Integration connection will be an efficient way to connect and interact between .NET and MongoDB.
- High-speed data reading and writing a high volume of data can be handled in MONGO DB, horizontal scaling, and flexible data structure.
- Role-based access and built-in security features such as encryption authentication are good for microservices architecture.
Code Example
For creating a connection string of Mongo BB, the "MongoClient" Class can be used to call the database using MongoDB. Driver Library connection is created.
using MongoDB.Driver;
var client = new MongoClient("localhost:27017");
var database = client.GetDatabase("your_database_name");
Input
[
{
"name": "XXX",
"age": 30
}
]
This is for inserting Json data into the MONGO DB.
var collection = database.GetCollection<BsonDocument>("your_collection_name");
var document = new BsonDocument { { "name":"XXX" }, { "age": 30 } };
await collection.InsertOneAsync(document);
To read or collect data from the MONGO DB.
var filter = Builders<BsonDocument>.Filter.Eq("name", "XXX");
var result = await collection.Find(filter).ToListAsync();
Update query code by filter option example.
var update = Builders<BsonDocument>.Update.Set("age", 31);
await collection.UpdateOneAsync(filter, update);
Remove documents from the collection or database.
var filter = Builders<BsonDocument>.Filter.Eq("name", "XXX");
await collection.DeleteOneAsync(filter);