简体   繁体   中英

Azure Document DB UpdateDoc

I am starting off with azure document db. I was trying to update an existing document. When I use the following query everything works:

dynamic Team2Doc = client.CreateDocumentQuery<Document>(documentCollection.DocumentsLink).Where(d => d.Id == "t002").AsEnumerable().FirstOrDefault();
Team2Doc.TeamName = "UPDATED_TEAM_2";
await client.ReplaceDocumentAsync(Team2Doc);

but if use the below code:

dynamic Team2Doc = client.CreateDocumentQuery<Document>(documentCollection.DocumentsLink).Where(d => d.TeamName== "team1").AsEnumerable().FirstOrDefault();
Team2Doc.TeamName = "UPDATED_TEAM_2";
await client.ReplaceDocumentAsync(Team2Doc);

I get this error:

"The best overloaded method match for 'Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentAsync(Microsoft.Azure.Documents.Document, Microsoft.Azure.Documents.Client.RequestOptions)' has some invalid arguments"

Is there anyway to retrieve a document by one of the properties and update the document?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM