Reputation: 2903
Can a Date.now type function be used in either map or reduce functions? Can it be used anywhere at all? More specifically, the view must not cache the Date.now value. Here is what I tested that only worked for the first run after a change to any view function:
function (doc){
var n = new Date();
if(doc.TimeStamp > n.getTime() - 30000){
emit(doc._id, doc);
}
}
Upvotes: 2
Views: 1115
Reputation: 1225
The view rows will be refreshed only when the particular doc gets updated. But you can request the view for that result: emit the doc.TimeStamp as key and request the view with ?startkey=timestamp where timestamp is the value of now.getTime() - 30000.
Upvotes: 3
Reputation: 1225
Yes. var now = new Date()
should work.
The condition must result in false
. You can test it with the view:
function (doc) {
var now = new Date()
var timestamp = now.getTime()
emit(timestamp,null)
}
It will respond something like
{
"total_rows":1,
"offset":0,
"rows" :[{
"id":"ecd99521eeda9a79320dd8a6954ecc2c",
"key":1429904419591, // timestamp as key
"value":null
}]
}
Make sure that doc.TimeStamp
is a number (maybe you have to execute parseInt(doc.TimeStamp)
) and greater then timestamp - 30000
Two words about your line of code emit(doc._id, doc);
:
doc._id
as key means maybe you doesn't need the view. Simply request the doc by GET /databasename/:id
. Also to include doc._id
in multipart keys or the value of the view row is mostly not necessary because its included in every row automatically as additional property. One valid reason would be when you want to sort the view over the doc ids.doc
as value is not recommended for performance reasons of the view. Simply add ?include_docs=true
when you request the view and every row will have an additional property doc
with whole doc in it. Upvotes: 2