Skip to content

hansamaligamage/cosmoscollection-create

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This sample code is going to show how to create a database, collection & documents in Azure Cosmos DB

Solution has built on Visual Studio 2017 on .NET Core 2.0

You can see code snippet to create the database, collection and document using Azure Cosmos DB SDK

You can go through this article and check how to trigger changes on Cosmos DB and execute an action using a Azure Function app, Azure Function App: Create a trigger on Cosmos DB

    public async static Task CreateDatabase (string database)
    {
        var db = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = database});
    }

    public async static Task CreateCollection(string database, string collection)
    {

        var databasecollection = await client.CreateDocumentCollectionIfNotExistsAsync(
        UriFactory.CreateDatabaseUri(database), new DocumentCollection { Id = collection });
    }

    public async static Task CreateDocument (string database, string collection, VehicleSpeed vehicleSpeed)
    {

        await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(database, collection),
        vehicleSpeed);
      
    }

    public async static Task CreateMultipleDocument(string database, string collection)
    {
        int documentCount = 500;
        for (int i = 0; i < documentCount; i++)
        {
            double speed = new Random().NextDouble() * 100;
            VehicleSpeed vehicleSpeed = new VehicleSpeed { VehicleNumber = "KJ -7788", City = "Colombo",
            Speed = speed};
            await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(database, collection),
            vehicleSpeed);
        }
    }

    private async static Task DeleteDocuments (string database, string collection)
    {
        await client.DeleteDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(database, collection));
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages