MongoDB

Find or Query documents or data from collection with C# and MongoDB

The Find and FindAsync methods are used to to retrieve data from a collection in MongoDB and all queries in MongoDB have the scope of a single collection.

Follow the Install and Connect to MongoDB step to connect to a running MongoDB instance.

The below code will finds documents whose Name field equals “Himen”.

 using System;  
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
FindDoc(args).Wait();
Console.WriteLine("Press Enter");
Console.ReadLine();
}
static async Task FindDoc(string[] args)
{
var _connectionString = "mongodb://localhost:27017";
var _client = new MongoClient(_connectionString);
var _database = _client.GetDatabase("blog");
var _collection = _database.GetCollection("users");
var _list = await _collection.Find("{Name : 'Test user'}").ToListAsync();
foreach(var _doc in _list)
{
Console.WriteLine(_doc);
}
}
}
}
MongoDB

Insert data or document into collection with C# and MongoDB

The InsertOneAsync method and the InsertManyAsync method are used to add documents to a collection in MongoDB. If documents to a collection that does not exist, MongoDB will create the collection.

We can use InsertManyAsync while inserting more than one document.

Follow the Install and Connect to MongoDB step to connect to a running MongoDB instance.

Below code will Insert a document into a collection named users. The operation will create the collection if the collection does not currently exist.

 using System;  
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
InsertDoc(args).Wait();
Console.WriteLine("Press Enter");
Console.ReadLine();
}
static async Task InsertDoc(string[] args)
{
var _connectionString = "mongodb://localhost:27017";
var _client = new MongoClient(_connectionString);
var _database = _client.GetDatabase("blog");
var _collection = _database.GetCollection("users");
var _document = new BsonDocument
{
{ "Name", "test user" },
{ "Email", "test@xxx.com" },
{ "Address1", "5619 other st" },
{ "City", "other city" },
{ "ZipCode", "12345" },
{ "Country", "test country" }
};
await _collection.InsertOneAsync(_document);
}
}
}
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");
}
}
}
MongoDB

Rebuild Indexes using Mongo shell

If you need to rebuild indexes for a collection you can use the db.collection.reIndex() method to rebuild all indexes on a collection in a single operation. This operation drops all indexes, including the _id index, and then rebuilds all indexes.

Example:-

 db.users.reIndex()