MongoDB.Driver.Extensions 2.0.2

MongoDB.Driver.Extensions

Nuget Nuget GitHub

MongoDB.Driver.Extensions is a library that extends MongoDB.Driver allowing you a set of functionality needed by common applications. The library is completely compatible with the .Net Standard 2.0

What can it be used for?

The idea behind this library is to make easier the common operation around a document you have persisted into MongoDb.

For example you have:

  • Save or update a document;
  • Insert a new document;
  • Update a document;
  • Delete a document;
  • Check if a document exists into your database;
  • Insert many documents in a single roundtrip;
  • Update many documents in a single roundtrip;
  • Retrieve a document by Id;
  • Handling pagination;
  • Count the number of documents;

All the methods available to do in the list above are available in both sync / async version and offers different parameters in order to change the amount of data to work.

##How to install it MongoDB.Driver.Extensions is available via NuGet, so to install is enough to do

PM> Install-Package MongoDB.Driver.Extensions

How to configure it

To use this library the first is to provide all the necessary information to the library. To do that the first thing to do is to create your document:

public class User : DocumentBase<ObjectId>
{
	public string Firstname { get; set; }
	public string Lastname { get; set; }
	public string Email { get; set; }
	public Guid CompanyId { get; set; }
}

In this example I'm using an ObjectId and database key, but of course you can change it with your favourite type (string, Guid, and so on).

Now is time to create your repository:

internal class UserRepository : RepositoryBase<User, ObjectId>
{
    public UserRepository( IMongoClient mongoClient)
        : base(mongoClient, "MyDatabase", "MyCollectionName")
    {
    }
}

The next step is the configuration and the IMongoClient :

var conf = new MongoDbDatabaseConfiguration();
conf.ConnectionString = "mongodb://localhost:27017

IMongoClient client = new MongoClient(conf.ConnectionString);

In you are in Asp.Net Core:

services.AddMongoDb(x => x.ConnectionString = "mongodb://mongodbhost:27017/sample");

services.AddSingleton<IRepository<User, ObjectId>, UserRepository>();


Now, in your service, you can do someting like this:

public class MyService : IMyService
{
	private readonly IRepository<User,ObjectId> userRepository;
	
	public MyService(IRepository<User,ObjectId> userRepository)
    {
    	this.userRepository = userRepository;
    }
    
    public Task<IPagedResult<User>> DoSomething(int pageIndex, int pageSize, Guid companyId)
    {
    	var request = new SimplePagedRequest();
    	request.PageIndex = pageIndex;
    	request.PageSize = pageSize;
    	
    	var filter = Builders<User>.Filter.Eq(x => x.CompanyId, companyId);
    	
    	return this.userRepository.GetPagedListAsync(request,filter);
    }
}

Sample

Take a look here

License

Imperugo.HttpRequestToCurl MIT licensed.

Contributing

Thanks to all the people who already contributed!

No packages depend on MongoDB.Driver.Extensions.

Version Downloads Last updated
2.0.2 14 03/04/2026
1.0.7 12 06/25/2026
1.0.6 10 06/25/2026
1.0.5 10 06/25/2026
1.0.4 11 06/25/2026
1.0.3 13 06/25/2026
1.0.2 13 06/25/2026
1.0.1 8 06/25/2026
1.0.0 15 06/25/2026