Couchbase 3 Couchnode 2 ViewQuery question



I use couchbase view to retrieve user that need to get a push notification on a mobile game.

The user receive push notification when I has not been offline for 3,7,14 or 21 days, based on a field “lastUpdate” stored in a json document.

for that I implemented the following map :

"map" : "function (doc, meta){ if(doc.type == 'user') { if (doc.lastUpdate && doc.token &&     doc.notificationsEnabled) { if (doc.token != '' && doc.notificationsEnabled == true) { emit(dateToArray(doc.lastUpdate),[,doc.retentionPN]); } }}}"

The results are retrieved using the following node.js code

var endDate         = moment().utc().add(delay,"seconds");
var startDate         = moment("2015-05-01 00:00:00");

var endDateArray     = endDate.toArray().slice(0,6); // couchbase and moment dont use same month/day start index
var startDateArray    = startDate.toArray().slice(0,6);


var query = ViewQuery.from('myview','withtoken_bydate').range(startDateArray, endDateArray, true).limit(100);

This works fine but I would like to check at the map level if one of the following subkey from the user profile is true, check d3 in case I check the d+3 interval, d7 in case I check the d+7 interval…

"retentionPN": {"d3": false,"d7": false,"d14": false,"d21": false}

Can do it as the viewQuery level , like for instance the following pseudo code

var query = ViewQuery.from('myview','withtoken_bydate').range(startDateArray, endDateArray, true).checkField("retention.d3",false).limit(100);

Hey nico_ad,

The simplest way to do this would actually be to do a query looking for specific keys of 3, 7, 14 and 21 days, then subsequently retrieving the document and checking the value of your retentionPN for each of the retrieved documents to identify if the date matches. This does mean you will pull down many users which fall on days which have no retentionPN enabled, however this is probably the most optimal way to do this. You could also change your view to avoid emitting keys for users which have no retentionPN values enabled at all.

Cheers, Brett