Updating documents

Updating documents

Documents can be updated in place by either the Replace or Upsert methods.

Replace() replaces an existing document within Couchbase, failing if it doesn’t exist. Upsert() replaces an existing document, creating it if it doesn’t exist.


 var person = document.Content;
 person.FirstName = "Tom";
 person.LastName = "Finnigan";

 if (bucket.Replace(document).Success)
 {
    var result = bucket.GetDocument<Person>("P1");
    if (result.Success)
    {
        person = result.Content;
        Console.WriteLine("Replaced document '{0}': {1} {2}", 
            document.Id, person.FirstName, person.LastName);
    }
 }
		

If the document didn’t already exist, the Replace call would fail and the results would not be “Tom” and “Finnigan”