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
<PackageReference Include="MongoDB.Driver.Extensions" Version="2.0.2" />
paket add MongoDB.Driver.Extensions --version 2.0.2
#r "nuget: MongoDB.Driver.Extensions, 2.0.2"
// 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
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 | Versions 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. |
-
net6.0
- Microsoft.Bcl.AsyncInterfaces (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- MongoDB.Driver (>= 2.18.0)
-
net7.0
- Microsoft.Bcl.AsyncInterfaces (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- MongoDB.Driver (>= 2.18.0)
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
|