Hi there,
This is indeed possible within Couchbase views!
I have done some testing and I think the best way to do this is to utilise the Map function capabilities.
So - the end product is, we need to take the document’s “Value”, and make sure that it is Above the ‘Start’ but Below the ‘end’. To do this, I have added another field into your JSON structure that explicitly contains the “value” we need to compare.
The JSON structure of my document now looks like this - with the added “Value” attribute:
{
"value": 103,
"start": 99,
"end": 110,
"country": "US"
}
So, to find if the “value” is between the “start” and “end”, we can write a simple Map function as follows:
function (doc, meta) {
if (doc.value > doc.start && doc.value < doc.end) {
emit(meta.id, doc.value);
}
}
Here, I am doing exactly as stated above: If the value is between the start and end, output the document’s key (id). I have also added ‘doc.value’ as the output value of the Map function, merely for fun. But if you also did this, you could use the doc.Value for many more things including stats etc.
I added two docs to my bucket, both with Values between the Start and End, and both are outputted, as they should be, when I execute my view code. This is what I see, and you should see, in your Admin UI:
http://i.rbin.co/rh0K
I hope this helps!
Robin.