Sample bucket with filter


#1

Hi.
I have a bucket of user actions and I would like to run queries that sample the recent documents of the bucket.
I also like to be able to filter with field=value in this sample.
What is the best way of doing that?
As I can see it, the only way of doing it is to create a view that emits all the document in the map function. I see everywhere that this is a bad practice, is there another way of doing it?

Thanks


#2

Here is an example using the beer-sample database that ships with Couchbase that does date filtering between two dates. Conditionals can be applied to emit only on specific fields and values etc…


function (doc, meta) {
var startDate=new Date(2010,6,21,12,0,0,0);
var stopDate=new Date(2010,6,23,12,0,0,0);
var itemDate=new Date(doc.updated);
if(itemDate.valueOf()>startDate.valueOf() &&
itemDate.valueOf()<stopDate.valueOf()){
emit(meta.id, null);
}
}


#3

@tgreenstein
I meant to filter in query time with a value that I get from a parameter.
Also, You emitted null to the value. So let’s say I want to sample 1000 documents. How can I see them?
Should I query the view with limit 1000 and then fetch 1000 documents? It doesn’t sound efficient to me.

Thanks