MongoODM.Net
MongoODM.Net is a .NET Object-Document Mapping (ODM) library designed to simplify the interaction between .NET applications and MongoDB databases. It provides a seamless way to map .NET objects to MongoDB documents, abstracting away the complexities of raw MongoDB queries and offering a more intuitive API for CRUD operations.
Features
- Entity Mapping: Define entity classes that represent MongoDB documents and map them to collections.
- CRUD Operations: Perform Create, Read, Update, and Delete operations on MongoDB documents using a simple API.
- Querying: Execute queries on MongoDB collections using LINQ expressions.
- Transactions: Perform multiple operations in a single transaction using the
Transaction
class.
- Aggregation: Execute aggregation pipelines on MongoDB collections using the
Aggregate
class.
- Indexes: Define indexes on MongoDB collections to improve query performance.
- Change Streams: Listen for changes in MongoDB collections using change streams.
Requirements
Installation
You can install the latest version of MongoODM.Net via NuGet. Run the following command in the Package Manager Console:
dotnet add package MongoODM.Net
Getting Started
Here's a quick example to get you started with MongoODM.Net:
- Define an entity class that represents a MongoDB document:
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
or
[CollectionName("Products")]
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
- Add your MongoDB connection string to the appsettings.json file:
{
"MongodbConfiguration": {
"Connections": [
{
"ConnectionString": "mongodb://localhost:27017",
"Databases": ["Database1","Database2"]
},
{
"ConnectionString": "mongodb://localhost:27018",
"Databases": ["Database3"]
}
]
}
}
- Add MongoODM.Net services to the DI container in your application
Startup.cs
or Program.cs
file:
services.AddMongoDbContext(configuration);
- Add the
MongoRepository<T>
to the DI container in your application Startup.cs
or Program.cs
file:
services.AddMongoRepository<Product>("Database1");
- Perform CRUD operations on MongoDB documents using the
MongoRepository<T>
in your service or controller:
public class ProductService: IProductService
{
private readonly MongoRepository<Product> _productRepository;
public ProductService(MongoRepository<Product> productRepository)
{
_productRepository = productRepository;
}
public async Task CreateProduct(Product product)
{
await _productRepository.AddAsync(product);
}
public async Task<Product> GetProductById(string id)
{
return await _productRepository.GetByIdAsync(id);
}
public async Task UpdateProduct(Product product)
{
await _productRepository.UpdateAsync(product);
}
public async Task DeleteProduct(string id)
{
await _productRepository.DeleteByIdAsync(id);
}
}
Find more examples of the use case in the github repository
git clone git@github.com:BiliksuunSamuel/MongoODM.Net.Example.git
License
This project is licensed under the MIT License.
Support
For any questions or issues,
please open an issue on GitHub or
contact us at
developer.biliksuun@gmail.com.
Authors