Help with a view

#1

Hello,

I am building a view to return some attribute values of a JSON document separated by comma “,”;

For example I have the following JSON:

{
“type”: “product”,
“dateTime”: “2015-07-20”,
“Category”: “test”,
“productID”: “8”,
“productName”: “p1”,
“valID”: “p1-1”,
“valValue”: “8”,
“userName”: “user5204”,
“userID”: “5204”,
“city”: “dublin”
}

I have created the following Map function ; (reduce function I leave it empty)

function (doc, meta) {
var date = new Date();
date.setDate(date.getDate() - 8); //// should be equal to 2015-07-20

MyDateString = date.getFullYear() + ‘-’ + (‘0’ + (date.getMonth()+1)).slice(-2) + ‘-’
+ (‘0’ + date.getDate()).slice(-2)
;

if(meta.type==“json” && doc.type==“product” && MyDateString == doc.dateTime) {

emit([doc.Category + doc.productID, doc.productName, doc.valID,doc.valValue
,doc.userName,doc.userID,doc.city] );
}
}

But the result I have is not the expected;
I am getting the following result;

p1-1,[“test”,“8”,“p1”,“p1-1”,“8”,“user5204”,“5204”,“dublin”],{
“type”: “product”,
“dateTime”: “2015-07-20”,
“Category”: “test”,
“productID”: “8”,
“productName”: “p1”,
“valID”: “p1-1”,
“valValue”: “8”,
“userName”: “user5204”,
“userID”: “5204”,
“city”: “dublin”
}

what I want to have is just the following;

“test”,“8”,“p1”,“p1-1”,“8”,“user5204”,“5204”,“dublin”

can someone help me ?
thanks
W

#2

Hey @couchbwiss,

I think your solution is close. Your emit is currently only returning the key of the key-value pair. What if you changed it slightly to look like the following:

emit(doc.Category + doc.productID, [doc.Category + doc.productID, doc.productName, doc.valID,doc.valValue,doc.userName,doc.userID,doc.city] );

Notice the key of every document returned by the view will be Category + productID. I personally put your sample data into my Couchbase bucket and created the view. The result looked as follows:

You can loop through the result set, only looking at the value which is the array. It seems to be what you wanted, unless I misunderstood something.

Please let me know.

Best,

1 Like
#3

@nraboy thank you :slight_smile: , I want to remove those brackets in the values so I just have “test”,“8”,“p1”,“p1-1”,“8”,“user5204”,“5204”,“dublin” returned.is it possible ?

#4

Hey @couchbwiss,

I am pretty sure that is not possible since you’re restricted to key-values in views. The SDKs wouldn’t be able to make sense of x amount of values coming back. The values must be valid JSON.

What was your use case for this requirement? Maybe I can help you find a different solution?

Best,

1 Like