Retrieving documents

Documents are retrieved by unique identifier (key). You can retrieve documents in these ways: consistent reads using the primary and inconsistent reads using replicas.

Regular reads

In most cases you can use regular reads for accessing your documents in Couchbase Server. In this case the client reads from the primary cluster node.

There are two methods defined (with many overloads) for reading documents, the first one takes the unique identifier of the document and returns an IOperationResult<T> object and the other takes the unique identifier and returns an IDocumentResult<T> object. Here is an example of using the latter:


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

The two perform essentially the same purpose; they retrieve a document from Couchbase by ID and cast the value to specified Type T. The IDocumentResult<T> and IOperationResult<T> interfaces are nearly symmetric. They both offer properties for checking whether or not the operation completed successfully (Success) and if the operation failed the server’s response status (Status) and an optional message with reason why.

Here is a table that describes the IDocumentResult<T> interface:

Table 1. IDocumentResult<T> properties
Name Type Description
Status ResponseStatus* The memcached response status from the server
Content IDocument<T> A type constrained representing a JSON document
Message string The message returned by the server or the client if the request could not be satisfied
Success Boolean True if the request was successful

Replica reads

In certain cases when availability is favored over consistency, replica reads can be used. A replica read returns data from the non-authoritative location (a node other than its master).