0

I am trying to update a document in Azure CosmosDB collection using ReplaceDocumentAsyc method provided by microsoft.azure.documentdb.core package. I get a 200 response when ReplaceDocumentAsyc is called but the document in collection itself is not getting updated. Here is the brief setup (relevent part of code) for reference.

    private DocumentClient Client
    {
        get
        {
            if (_client == null)
            {
                _client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            }

            return _client;
        }
    }

    Client.ReplaceDocumentAsync(documentSelfLink, doc).ContinueWith(response =>
    {
        return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
    });

The code seems fine to me and I believe it was able to update the collection before. Has anyone else faced similar issue?

1
  • Is your collection partitioned? Does the doc object contain the id property of the document? Commented Sep 6, 2018 at 8:09

1 Answer 1

2

Please refer to my working code:

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using System;

namespace JayTestDocumentDB
{
    class Program
    {
        private static DocumentClient client;
        private static string EndpointUrl = "https://***.documents.azure.com:443/";
        private static string AuthorizationKey = "***";
        private static string databaseId = "db";
        private static string collectionId = "coll";

        static void Main(string[] args)
        {
            client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            var options = new FeedOptions
            {
                MaxItemCount = 100
            };      
            FeedResponse<Document> doc = client.CreateDocumentQuery<Document>(uri, options).AsDocumentQuery().ExecuteNextAsync<Document>().Result;

            foreach (Document d in doc)
            {
                Console.WriteLine(d);
                d.SetPropertyValue("name","jay");

                client.ReplaceDocumentAsync(d.SelfLink, d).ContinueWith(response =>
                {
                    Console.WriteLine(response.Result.StatusCode);
                    //return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
                });
                break;
            }
            Console.ReadLine();
        }
    }                    
}

My package version of microsoft.azure.documentdb.core is 2.0.0-preview2.

Any concern,please let me know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.