MongoDB

Installing and Connecting MongoDB Driver for .NET

MongoDB Driver: An updated .NET driver offering a full asynchronous stack.

BSON Library: A standalone BSON library with a serialization infrastructure that you can use to build high-performance serializers.

Core Library: A new core library upon which MongoDB .NET Driver is built. Users can use the new core library to build alternative or experimental high-level APIs.

To install MongoDB.Driver, run the following command in the  Package Manager Console.

 PM> Install-Package MongoDB.Driver -Version 2.0.1   

Or, through NuGet Package Manager > Manage NuGet Packages for Solutions

Connect to MongoDB
Add the following using statements in your C# project files.

 using MongoDB.Bson;  
using MongoDB.Driver;

Include the below code in your project to create a client connection to a running mongod instance and use the blog database and users collection.

 using System;  
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
ConnectDB(args).Wait();
Console.WriteLine("Press Enter");
Console.ReadLine();
}
static async Task ConnectDB(string[] args)
{
var _connectionString = "mongodb://localhost:27017";
var _client = new MongoClient(_connectionString);
var _database = _client.GetDatabase("blog");
var _collection = _database.GetCollection("users");
}
}
}

Leave a comment