Azure Document DB UpdateDoc Azure Document DB UpdateDoc azure azure

Azure Document DB UpdateDoc


The where clause is trying to query the property TeamName which does not exist in Document class.

Changing the type of the queryable to your data model should fix it.

For example, say you have the following data model:

public class EmployeeDocument : Document{        // Other properties that you may have similarly defined ....     public class string TeamName      {        get        {            return this.GetValue<string>("TeamName");        }        set        {            this.SetValue("TeamName", value);        }     }}

Then you can modify your query like this:

var team2Doc = client.CreateDocumentQuery<EmployeeDocument>(documentCollection.DocumentsLink).Where(d => d.TeamName== "team1").AsEnumerable().FirstOrDefault();team2Doc.TeamName = "UPDATED_TEAM_2";await client.ReplaceDocumentAsync(team2Doc);

Note that you have to use the EmployeeDocument, instead of the Document class, while creating the document queryable. That will let you query on EmployeeDocument properties.

SQL Version

Creating a document model for each of your existing data models may not be feasible if you have a large number of data models. In that case you may want to try out the SQL query syntax.

Refer to Aravind's answer in this post. The example he uses is for deleting documents, but it can be easily modified to update them too.


You can also create a model with Id:

public class Employee{                 [JsonPropery("id")]     public class string Id { get; set; }     public class string TeamName { get; set; }}

And then replace the document using it's Id:

var employee= client    .CreateDocumentQuery<Employee>(documentCollection.DocumentsLink)    .Where(d => d.TeamName== "team1")    .AsEnumerable()    .FirstOrDefault();employee.TeamName = "team2";var documentUri = UriFactory.CreateDocumentUri(databaseName, collectionName, employee.Id);await client.ReplaceDocumentAsync(employee);