Giter Club home page Giter Club logo

webjobs.mongo's Introduction

Azure Functions MongoDB Extensions

This repo contains MongoDB binding extensions for the Azure WebJobs SDK. See the Azure WebJobs SDK repo for more information. The binding extensions in this repo are available as the Mallow.WebJobs.Mongo nuget package.

Usage

Input binding

Example azure function that can be used to obtain one document via it's ID.

public static Task<IActionResult> GetDocumentById
(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
    [Mongo(DatabaseId = "TestDb", CollectionId = "Collection", ConnectionString = "%ConnectionString%", Id = "{Query.id}")] Document input
)
{
    if (input != null)
    {
        return Task.FromResult<IActionResult>(new OkObjectResult(input));
    }

    return Task.FromResult<IActionResult>(new BadRequestObjectResult("Document not found"));
}

Example azure function that can be used to obtain multiple documents using filter. Where filter is standard MongoDB query.

public static Task<IActionResult> GetDocuments
(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
    [Mongo(DatabaseId = "TestDb", CollectionId = "Collection", ConnectionString = "%ConnectionString%", Filter = "{Query.filter}")] IEnumerable<TestDocument> input
)
{
    return Task.FromResult<IActionResult>(new OkObjectResult(input));
}

Output binding

Example azure function that can be used to create multiple new documents.

public static async Task<IActionResult> InsertNewDocuments
(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage  req,
    [Mongo(DatabaseId = "TestDb", CollectionId = "Collection", ConnectionString = "%MongoConnectionString%")] IAsyncCollector<AsyncCollectorTestDocument> collector
)
{
    var documents = await req.Content.ReadAsAsync<Document[]>();
    foreach (var document in documents)
    {
        await collector.AddAsync(document);
    }

    return new OkObjectResult(new {Status = "OK"});
}

Example azure function that can be used to create/replace multiple new documents.

public static async Task<IActionResult> InsertNewDocuments
(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage  req,
    [Mongo(DatabaseId = "TestDb",
           CollectionId = "Collection",
           ConnectionString = "%MongoConnectionString%",
           Mode = InsertMode.CreateOrReplace)] IAsyncCollector<AsyncCollectorTestDocument> collector
)
{
    var documents = await req.Content.ReadAsAsync<Document[]>();
    foreach (var document in documents)
    {
        await collector.AddAsync(document);
    }

    return new OkObjectResult(new {Status = "OK"});
}

License

the MIT License

webjobs.mongo's People

Contributors

mallow42 avatar

Forkers

zdenekjurka

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.