How to retrieve a couchbase document by passing its ID using Linq


#1

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.

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.


#2

@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:

Please let me know if you have more questions.

Brant