2017-07-23 1 views
0
// I am using code like below 
Document doc = client.CreateDocumentQuery<Document>(collectionLink) 
          .Where(r => r.Id == "doc id")  
          .AsEnumerable() 
          .SingleOrDefault(); 

doc.SetPropertyValue("MyProperty1", "updated value"); 
Document updated = await client.ReplaceDocumentAsync(doc); 

Want "MyProperty2" aus dem Dokument zu entfernen. Wie macht man das?Azure Cosmos Db Dokument - will mit einer entfernt Eigenschaft verwendet replacedocumentasync im Dokument

+0

Bitte sagen Sie, ob Microsoft.Azure.Document eine Möglichkeit hat, eine Eigenschaft aus dem Dokument zu entfernen – user1853141

Antwort

2

Es scheint, dass Sie MyProperty1 Eigenschaft aktualisieren und MyProperty2 Eigenschaft aus Ihrem Dokument entfernen möchten, der folgende Beispielcode dient als Referenz.

private async Task updateDoc() 
{ 
    string EndpointUri = "xxxxx"; 
    string PrimaryKey = "xxxxx"; 
    DocumentClient client; 

    client = new DocumentClient(new Uri(EndpointUri), PrimaryKey); 

    Document doc = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri("testdb", "testcoll")) 
       .Where(r => r.Id == "doc5") 
       .AsEnumerable() 
       .SingleOrDefault(); 

    //dynamically cast doc back to your MyPoco 
    MyPoco poco = (dynamic)doc; 

    //Update MyProperty1 of the poco object 
    poco.MyProperty1 = "updated value"; 

    //replace document 
    Document updateddoc = await client.ReplaceDocumentAsync(doc.SelfLink, poco); 


    Console.WriteLine(updateddoc); 
} 

public class MyPoco 
{ 
    public string id { get; set; } 
    public string MyProperty1 { get; set; } 
} 

Mein Dokument: enter image description here Aktualisiert: enter image description here

Edit:

dieses "MyProperty3" entfernen würde und "MyProperty4" als auch.

Wie Sie bereits erwähnt haben, wäre das Festlegen einer Eigenschaft mit null auch ein Ansatz.

Document doc = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri("testdb", "testcoll")) 
      .Where(r => r.Id == "doc5") 
      .AsEnumerable() 
      .SingleOrDefault(); 

doc.SetPropertyValue("MyProperty2", null); 

//replace document 
Document updateddoc = await client.ReplaceDocumentAsync(doc.SelfLink, doc); 
+0

Danke Fred. Dies würde aber auch "MyProperty3" und "MyProperty4" entfernen, die in MyPoco vorhanden sind. – user1853141

+0

Hi @ user1853141, habe ich die Antwort bearbeitet. –

0

Danke für die Antwort. Du hast verstanden, wonach ich gefragt habe. Ich wollte MyPoco nicht als Microsoft.Azure.Document verwenden ist am flexibelsten. Wenn Sie diesen Ansatz verwenden, wird auch MyProperty3 entfernt, wenn es in MyPoco vorhanden ist. Nur mit der Document-Klasse arbeiten. eachDoc.SetPropertyValue ("MyProperty2", null);

Verwandte Themen