I have the following object in Couchbase:
{
"postReplyId": "Reply_9AE1F47E585522FD1D2EFFEA7671C0855BBFDA991698B23886E37D1C65DAC8AF_1375468399745",
"userId": "User_9AE1F47E585522FD1D2EFFEA7671C0855BBFDA991698B23886E37D1C65DAC8AF",
"postId": "Message_9AE1F47E585522FD1D2EFFEA7671C0855BBFDA991698B23886E37D1C65DAC8AF_1375457606125",
"post_reply_message": "Testing",
"attachments": {
"images": [
],
"audio": [
],
"videos": [
]
},
"upVoters": [
],
"downVoters": [
],
"upVotes": 0,
"report": 0,
"reporters": [
],
"timestamp": 1375468399745,
"mtype": "reply"
}
I would like to have a view and return all the posts created during the last 30 minutes
by the user x
I did:
function (doc, meta) {
if(doc.mtype == "reply") {
var dt = new Date();
if((dt.getTime() - doc.timestamp) < 1800000 )
emit(doc.userId, doc);
}
}
and i pass the userIds as multiple keys in the URL, but I get old results
Can someone suggest a solution?
A view runs as documents are added/modified, and only upon request or when automatically updated. It doesn't rerun constantly, and more importantly, it does not re-run for already added documents. So, as written your view would only contain old results.
You need to emit all documents and include the timestamp as part of the emit, so that you can use that as part of the query to the view (a time range).
So, in your emit function, you might instead (untested code):
Then your query might be like this range (broken into several lines for readability):
Although
endkey
wouldn't strictly be necessary, I've left it in for clarity.Because of the way Couchbase views work, a view may not always contain all data though (from here):
Also, note that documents are not immediately added to the view by default. Read this for more information.