Creating documents
The IDocument<T> interface represents an abstraction for working with documents in Couchbase Server.
The default implementation of IDocument<T>is the Document<T> class, which provides the following properties:
| Name | Type | Description |
|---|---|---|
| Id | string | The unique identifier for the document |
| Cas | ulong | The check and set (CAS) value for enforcing optimistic concurrency |
| Expiry | uint | The time-to-live (TTL) of the document in milliseconds. A value of zero or less for infinite lifetime. |
| Content | Generic T | The actual document value to store. This can be a scalar value, an object, or a dynamic type. |
The following code snippet shows how to insert a document into Couchbase:
var document = new Document<Person>
{
Id = "P1",
Content = new Person
{
FirstName = "John",
LastName = "Adams",
Age = 21
}
};
var result = bucket.Insert(document);
if (result.Success)
{
Console.WriteLine("Inserted document '{0}'", document.Id);
}
This example uses the Insert() method, which creates a document if it doesn’t exist, failing if it does. The following table shows the methods that are available for storing documents in Couchbase:
| Name | Description |
|---|---|
| Insert | Inserts a document, failing if it already exists |
| Replace | Replaces a document, failing if doesn’t exist |
| Upsert | Updates a document if it exists, otherwise inserts the document |
If the Cas or Expires value is set, they will be honored for each operation.