Rodrigo Asensio
Rodrigo Asensio

Reputation: 2792

Multiple optional keys in couchbase

my map function

function(doc, meta) {
  emit([doc.city, doc.region], doc.id);
}

my query

?keys=[["New York", "Northeast"]]

this will emit:

{ 
  city: 'New York',
  region: Northeast
}

If I want use the same map function getting only those cities from Northeast it won't work. is there any way to match only 1 of the keys ? Like...

?keys=[[null, "Northeast"]]

and outputing

[{ 
  city: 'New York',
  region: 'Northeast'
},
{
  city: 'Boston',
  region: 'Northeast'
}]

Upvotes: 2

Views: 1299

Answers (1)

Tug Grall
Tug Grall

Reputation: 3510

When you are using a query/view into Couchbase you can only read the key from left to right.

So if you want to be able to query on each "part" you need to create a new view that emit (so you can if you want do the 2 emit in the same view or create 2 views)

emit(doc.region, doc.city);

And query it for example using

?startkey=["Northeast"]&endkey["Northeast",{}]

Also,as you can see I do not emit the doc.id, this is not necessary because the doc.id is always emitted by Couchbase views.

Upvotes: 6

Related Questions