.Net client 1.2.6: Using Views with multiple key values

Hi,

I have created a development view in Couchbase bucket with data of the following format:

{
"productId": "1-124DH9",
"productCode": "PCODE",
"type": "Example.Product"
}

The view created is -
function (doc, meta) {
if (doc.type && doc.type == Product') {
emit([doc.productId], null);
}
}

After storing data into the bucket and running this view I get all the data related to this productCrmId.
View result –
{"total_rows":200,"rows":[

{"id":" Product#1-124DH9","key":["1-124DH9"],"value":null},
{"id":" Product#1-12VEXP","key":["1-12VEXP"],"value":null},
...
...
]

Now my problem is that I want to get this view result in my code filtered by list of keys.

The GetView(designDoc, view, true).Keys(new string[] {}) method accepts multiple keys for the same
compound index.

But I have just one key per index, and I cannot find any method using which I can
pass multiple key values and get a list of objects back from the view.

Basically, I want 2 objects returned if I search a view using the following 2 keys - "1-124DH9" and "1-12VEXP".

Any pointers or suggestions would be great!

TIA

1 Answer

« Back to question.

Hello,

Just curious, why do you want to put the key in an 1 value array?

 emit(doc.productId, null);

And when you query you can do:

 ?keys=["1-12VEXP","1-124DH9"]

If you really keep the emit in an array - emit([doc.product]); -, you will need to do this key of arrays to get multiple documents:

?keys=[["1-12VEXP"],["1-124DH9"]]

So in your code you either have to create a list of String or single array String. (from a JSON point of view)

Regards
Tug
@tgrall

 public List<T> Search<T>(Type type, string indexName, List<string> values) where T : class
    {
      string[][] newKeys = values.Select(x => new string[] { x }).ToArray();
      string jsonValue= JsonConvert.SerializeObject(newKeys); <code>
 
<code>var result = client.GetView<Product>("dev_Entitlement", "by_productCrmId", true).Keys<>();

What type i do need to specify for keys<>(jsonValue) such that i can pass my formatted json value .
Thanks
Sunil singh

Thanks it is working now updated to emit(doc.productId, null);
Regards
sunil singh