MongoDB.Driver.Extensions 2.0.2

dotnet add package MongoDB.Driver.Extensions --version 2.0.2                
NuGet\Install-Package MongoDB.Driver.Extensions -Version 2.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MongoDB.Driver.Extensions" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoDB.Driver.Extensions --version 2.0.2                
#r "nuget: MongoDB.Driver.Extensions, 2.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MongoDB.Driver.Extensions as a Cake Addin
#addin nuget:?package=MongoDB.Driver.Extensions&version=2.0.2

// Install MongoDB.Driver.Extensions as a Cake Tool
#tool nuget:?package=MongoDB.Driver.Extensions&version=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!

<a href="https://github.com/imperugo/MongoDB.Driver.Extensions/graphs/contributors"> <img src="https://contributors-img.web.app/image?repo=imperugo/MongoDB.Driver.Extensions" /> </a>

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MongoDB.Driver.Extensions:

Package Downloads
Elsa.MongoDb

Provides MongoDB implementations of various abstractions from various modules.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on MongoDB.Driver.Extensions:

Repository Stars
elsa-workflows/elsa-core
A .NET workflows library
Version Downloads Last updated
2.0.2 45,999 12/5/2022
1.0.7 14,494 12/12/2020
1.0.6 24,064 10/24/2020
1.0.5 25,561 11/1/2019
1.0.4 1,756 3/6/2019
1.0.3 1,051 3/5/2019
1.0.2 1,011 3/5/2019
1.0.1 1,152 3/5/2019
1.0.0 1,177 3/5/2019