Walle.Components.MongoDB
Installations
Package Manager:
Install-Package Walle.Components.MongoDB
.NetCore CLI:
dotnet add package Walle.Components.MongoDB
Configurations
- add the follow section into
appsettings.json
:
{
"MongoDBConfig": {
"ConnectionStr": "mongodb://127.0.0.1:27017/walle",
"DatabaseName": "walle"
}
}
- register dependency in your
Startup.cs
private IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureMongoDB(Configuration);
}
Samples
- Inherited an entity from MongoEntity which will be a collection model.
using Walle.Components.MongoDB;
namespace Demo
{
public class ActivityModel : MongoEntity
{
public string Name { get; set; }
public string Description { get; set; }
}
}
- using
IMongoDBCollection<T>
to do IQueryable
operations with LINQ.
using Demo;
using Walle.Components.MongoDB;
namespace Demo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ActivityController : ControllerBase
{
private IMongoDBCollection<ActivityModel> ActivityCollection { get; }
public ActivityController(IMongoDBCollection<ActivityModel> collection)
{
this.ActivityCollection = collection;
}
[HttpGet]
public RespList<ActivityModel> GetAll()
{
RespList<ActivityModel> resp = new RespList<ActivityModel>();
try
{
var sources = ActivityCollection.AsQueryable()?.ToList();
resp.Message = $"success.";
resp.Data = sources;
}
catch (Exception ex)
{
resp.Code = (int)RespCode.Exception;
resp.IsSuccess = false;
}
return resp;
}
}
}
- using
IMongoDBClient
to get the mongodb client for more operations.
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Walle.Components.MongoDB;
namespace Demo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DemoController : ControllerBase
{
private IMongoDBClient Client { get; }
public DemoController(IMongoDBClient Client)
{
this.Client = Client;
}
[HttpGet]
public RespList<ActivityModel> GetAll()
{
RespList<ActivityModel> resp = new RespList<ActivityModel>();
try
{
var results = Client.Find<ActivityModel>(p => p.Name == "Misaya").ToList();
resp.Data = results;
}
catch (Exception )
{
var msg = "Exception occured when query activity.";
}
return resp;
}
}
}