Skip to main content

Posts

Showing posts from September, 2019

Working with MongoDB in .Net Core (Part 5) - Mongo Queries

Mongo Aggregation pipeline Queries //Pipeline Query use Collection; db.getCollection(collectionName).aggregate( [ { " $match " : { " {field1} " : { " $eq " : " string " }, " {field2} " : { " $eq " : " string " } }, }, { " $group " : { " _id " : " $field " , " Count " : { " $sum " : 1.0 }, " Results " : { " $addToSet " : " $field " } } }, { " $sort " : { " Count " : -1.0 } }, { " $project " : { " count " : 1.0, " _id " : 1.0, " AccountId " : 1.0 } } ],

SwashBuckle - Swagger Open API Documentation

Swashbuckle Open API Swagger is Open API documentation used to understand the various methods and the request/responses of various endpoints. It generates the Web API SDK for clients. Swashbuckle .AspNetCore is an open-source project for generating Swagger documents for ASP.Net Core Web APIs.  Another open-source project is  NSwag  for generating swagger documents. Enable Swagger in .NET Core Apps -  Note - Install the  Swashbuckle.AspNetCore NuGet package  into your solution.Don't install Swashbuckle.AspNetCore.Swagger package) Add the below to the Configure() Method -  app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "AppName"); }); using Swashbuckle.AspNetCore.Examples ; using Swashbuckle.AspNetCore.Swagger ; public void ConfigureServices (IServiceCollection services) { // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { var appVersion

Working with MongoDB in .Net Core (Part 3) - Definitions and Builders

                 Mongo Queries - C# Driver Writing Complex Mongo Queries using Aggregation - Builders and BsonDocuments Builders -  1 2 3 4 5 6 7 8 9 10 11 12 //Filter Definition Builder var builder = Builders<T>.Filter; var filter = builder.Ne(_=>_.{field}, true ); filter = filter & builder.Eq(_=>_.{field}, value ); filter = filter & builder.Gte(_=>_.{field}, value ); filter = filter & builder.Lte(_=>_.{field}, value ); filter = filter & builder.In(_=>_.{field}, value ); filter = filter & builder.In(_=>_.{field}, new List< string > { value1, value2}); filter = filter & builder.AnyEq(_=>_.{field}, value ); filter = filter & builder.Or(_=>_.{field}, value ); filter = filter & builder.And(_=>_.{field}, value ); filter = filter & builder.ElemMatch(_=>_.{field}, t => t.Id == Id, t.Key == Key); 1 2 3 4 5 6 7 8 9 //Sort Definition Builder var sortfilter = Builders<T>.Sort.Descen

Working with MongoDB in .Net Core (Part 2) - Mongo Collection Base

Mongo Repository - C# Here we introduce an encapsulation called BaseRepository which every other repository created in the application uses to perform Mongo database Operations. BaseRepository 1 2 3 4 5 6 7 8 9 public abstract class BaseRepository <T> { private readonly IMongoCollection<T> _collection; public BaseRepository (IMongoConnectionManager connectionManager, string collectionName) { _collection = connectionManager.GetCollection<T>(collectionName) } } Mongo Database Operation Types //FindAsync - GetMany and GetSingle protected async Task<T> GetSingle(FilterDefinition<T> filter, FindOptions<T,T> options = null ) { var retval = await _collection.FindAsync(filter, options).ConfigureAwait( false ); return retval.FirstOrDefault(); } protected async Task<T> Get(FilterDefinition<T> filter, SortDefinition<T> sort = null ) { var retval = await _collection.FindAsync(filter, new

Working with MongoDB in .Net Core (Part 1) - Mongo Connection Manager

Connecting to Mongo Database C# Mongo DB is a NoSQL document-oriented database which stores data in a JSON-like format. Mongo DB stores the data in a binary-encoded format called BSON. Every application connecting to Mongo DB will have a Mongo connection manager to create Mongo Clients and connect to database and collection to perform CRUD operations. Here is a sample of how a Mongo Connection manager would look like, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 using MongoDB.Bson ; using MongoDB.Bson.Serialization.Conventions ; using MongoDB.Driver ; public class MongoConnectionManager : IMongoConnectionManager { public IMongoClient client { get ; set ;} private readonly MongoDBConfigSettings _mongoDBSettings; //Setup Mongo Client in the connection Manager constructor when the instance is created, so that it can be dependency injected into other classes public MongoConnectionManager (IOptions<MongoDBConfigSettings> mongoDBSettings) { m