Hi,
I want to get the couchbase document with all its fields . I am passing the ID using the Meta() function.
I refered to this article.
The META() function
================
For every document Couchbase Server will also store another document containing the meta data describing that document. Meta-data includes the following fields:
- Id - the primary key for the document
- CAS - Compare and Swap value
- Flags - SDK specific type encoding (internal use only)
- Type - the type of document stored in Couchbase (will always be JSON for N1QL)
There is additionally meta-data stored within couchbase for each document (TTL and Sequence Number), but this is not available from the META function.
##META() example
The simplest usage of the META() function is to query it directly:
var query = (from meta in context.Query<DocumentMetadata>()
select N1QlFunctions.Meta(meta)).
Take(1);
In this example, we are simply retrieving the meta-data from the first document returned in the beer-sample bucket. The "raw" results would look something like this:
This file has been truncated. show original
In that I found the query below.
var beers = (from b in context.Query<Beer>()
where b.Type == "beer"
select new {name = b.Name, id = N1QlFunctions.Meta(b).Id});
Its working, but I want to retrieve all the fields in the document and not just the Name. Can you suggest me how I should modify the query.
1 Like
@nithisha
Try this LINQ query and see if that works for you:
var beers = from b in context.Query<Beer>()
where b.Type == "beer"
select new {beer = b, id = N1QlFunctions.Meta(b).Id};
Also, you should note that we do offer some integrated, behind-the-scenes support for this if you use the experimental new change tracking feature in version 1.1. Assuming you are using the ID to perform updates, you can do this:
context.BeginChangeTracking();
var beers = from b in context.Query<Beer>()
where b.Type == "beer"
select b;
foreach (var beer in beers) {
// modify the document
}
context.SubmitChanges();
In this case, the ID is automatically tracked internally, so that you don’t have to. However there are some POCO requirements. All of this is documented here:
Change Tracking (Experimental Developer Preview)
================================================
Change tracking allows for reading documents from Couchbase using LINQ queries, modifying the documents in memory, and then saving the documents back as a unit of work. Only documents which are modified are saved.
Note that this feature is currently an experimental feature being offered as a developer preview. The API is subject to change, and backwards compatibility following the semver versioning rules is not guaranteed.
##Preparing Your Document POCOs
Many change tracking systems for database access layers work based on comparing records. As each record is read two copies are kept in memory, the original and the current record. When it's time to save changes, the two records are compared to check for differences that need to be saved.
With Couchbase's document-based store, this can result in a very large memory footprint. As each document can easily be several kilobytes, keeping two copies of each document in memory is doubling the required memory. To address this, Linq2Couchbase doesn't use the two copy comparison approach. Instead, only a single document is kept in memory, and it is flagged as dirty anytime a property is changed.
This support is provided using dynamic proxies. As each document is read and new objects are created, instead of directly creating your POCO object a proxy of your POCO is created instead. The properties are overridden dynamically so that changes to the property can trigger the dirty flag. To support this feature, there are some requirements that must be met.
1. Document POCOs must not be sealed. This allows the proxy to inherit from them.
2. Every property on the document must be declared virtual (Overridable in VB). This allows each property to be overriden by the dynamic proxy. Any property which is not virtual will not have change tracking applied to it.
3. For lists and arrays, the properties must be declared as the type IList<T> or ICollection<T>. If they are not then that branch of the document tree will not have change tracking.
4. These rules must apply to the entire document tree, meaning any child objects of the main document object must also have virtual members, etc.
```
public class DocumentRoot
This file has been truncated. show original
Please let me know if you have more questions.
Brant
2 Likes