0
votes

I'm migrating an application from Azure SQL DB to Cosmos DB and am having trouble storing documents in CosmosDB.

My application is written in C# using the Microsoft.Azure.DocumentDB library. When I use the method CreateDocumentAsync(Uri, object) from the library Microsoft.Azure.DocumentDB to insert a record it goes throught without any error. When I query the database using the Azure CosmosDB Data explorer I do not see any records although in the mongo shell the count() states the records are there and the .find() gives an error "Unknown server error occurred when processing this request."

I used the library Microsoft.Azure.DocumentDB to make a crud of sorts. I tested it with the emulator wich worked. however when I was trying to couple a online cosmosDB I came upon the problem that the moment that I insert a document via the method CreateDocumentAsync(Uri, object) from the library I no longer am able to see inserted documents in the comosDb dataexplorer.

I tried to insert it without an id and tried to insert it with a objectId _id however I kept getting the same problem.

When I look in the collection via the mongo shell I do see that a document has been added but when I used the db.colleciton.find() I get the error: "Unknown server error occurred when processing this request." Code further down the chain is able to retrieving the documents. Am I missing something? do I need to set a setting on in azure dB or is this a known issue of the library?

class SalesOrder
{
    public string Id { get; set; }

            public string PurchaseOrderNumber { get; set; }

            public DateTime OrderDate { get; set; }

            public string AccountNumber { get; set; }

            public decimal SubTotal { get; set; }

            public decimal TaxAmt { get; set; }

            public decimal Freight { get; set; }

            public decimal TotalDue {get; set;}
    }

class Program
{
    private static readonly string endpointUrl = "endpoint";

    private static readonly string authorizationKey = "authorizationKey";

    private static readonly string databaseId = "databaseId";

    private static readonly string collectionId = "collectionId";

    private static DocumentClient client;

    static void Main(string[] args)
    {
        using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
        {
            var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            Insert(collectionLink).Wait();
        }
    }

    private static async Task Insert(Uri collectionLink)
    {
        var orders = new List<object>();

        orders.Add(new SalesOrder

        {
            Id = "POCO1",

            PurchaseOrderNumber = "PO18009186470",

            OrderDate = new DateTime(2005, 7, 1),

            AccountNumber = "10-4020-000510",

            SubTotal = 419.4589m,

            TaxAmt = 12.5838m,

            Freight = 472.3108m,

            TotalDue = 985.018m
        });



        orders.Add(new SalesOrder

        {

            Id = "POCO2",

            PurchaseOrderNumber = "PO15428132599",

            OrderDate = new DateTime(2005, 7, 1),

            AccountNumber = "10-4020-000646",

            SubTotal = 6107.0820m,

            TaxAmt = 586.1203m,

            Freight = 183.1626m,

            TotalDue = 4893.3929m,
        });

        foreach (var order in orders)

        {

            Document created = await client.CreateDocumentAsync(collectionLink, order);



            Console.WriteLine(created);

        }
    }
}
1
Can you also show us how you are trying to retrieve those documents in the explorer? You mentioned count() and find() which I don't think are part of the UI. Can you tell us what the error that you get at the bottom status strip is?Nick Chapsas
sorry for the poor explanation the methods .count() and .find() methods where used in the mongo shell. I will update it in the question.Roland Visser
Oh are you using the MongoDB API?Nick Chapsas
No I am using the library Microsoft.Azure.DocumentDB wich uses a DocumentClient to create documents in ComosDbRoland Visser
But your account is targeting Mongo API and not SQL API, correct?Gaurav Mantri

1 Answers

0
votes

The problem is that you have a Cosmos DB account which is using the MongoDB API but you are trying to create data using the SQL API SDK.

If you want to use the MongoDB API when you need to use a MongoDB SDK, not the Cosmos DB one.

However, if you are migrating from Azure SQL and you don't have a specific reason to use the MongoDB API then you should just use the CosmosDB SQL API and the SDK you are already using. If you do that your problem will go away.

The reason why it occurs is because the SQL API SDK is creating SQL API looking files so the CosmosDB MongoDB interface can't deal with them in the UI.