Coder Social home page Coder Social logo

walle.components.mongodb's Introduction

Walle.Components.MongoDB

badge NuGet Badge

Installations

Package Manager:

Install-Package Walle.Components.MongoDB  

.NetCore CLI:

dotnet add package Walle.Components.MongoDB 

Configurations

  1. add the follow section into appsettings.json :
{
  "MongoDBConfig": {
    "ConnectionStr": "mongodb://127.0.0.1:27017/walle",
    "DatabaseName": "walle"
  }
}
  1. 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

  1. 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; }

    }
}
  1. 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;
        }
    }
}
  1. 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;
        }

    }
}

walle.components.mongodb's People

Contributors

misaya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.