通过时间戳和用户id筛选职位 [英] Filtering posts by timestamp and userId

查看:206
本文介绍了通过时间戳和用户id筛选职位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Couchbase以下对象:

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"
}

我想有一个观点和由用户 X 30分钟内产生的所有帖子>

I would like to have a view and return all the posts created during the last 30 minutes by the user x

我所做的:

function (doc, meta) {
  if(doc.mtype == "reply") {
    var dt = new Date();
    if((dt.getTime() - doc.timestamp) < 1800000 )
    emit(doc.userId, doc);
  }
}

和我传递的用户id作为URL多个键,但我老效果

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).

因此​​,在你的表达函数,你可能会改为(未经测试code):

So, in your emit function, you might instead (untested code):

function (doc, meta) {
  if (doc.mtype === "reply") {
    // dt will be when the document is processed by the view, which may
    // not when the document was added.
    var dt = new Date();  
    var year = dt.getFullYear();
    var month = dt.getMonth() + 1; // make month 1-12
    var day = dt.getDate();
    var hours = dt.getHours();
    var minutes = dt.getMinutes();
    var seconds = dt.getSeconds();

    // emit the full key, including the user id and the date of the document.
    emit([doc.userId, year, month, day, hours, minutes, seconds], doc._id);
  }
}

那么你的查询可能是这样的范围内(分为几行以便于阅读):

Then your query might be like this range (broken into several lines for readability):

/salamis_db/_design/docs/_view/by_user_date?
     startkey=["the-userId", 2013, 8, 7, 10, 30, 0]
     &endkey=["the-userId", 2013, 8, 7, 11, 00, 00]

虽然 endkey 不严格是必要的,我已经离开它的清晰度。

Although endkey wouldn't strictly be necessary, I've left it in for clarity.

由于道路Couchbase意见工作,一种观点可能并不总是包含所有,虽然数据(的这里):

Because of the way Couchbase views work, a view may not always contain all data though (from here):

不管陈旧参数,文件只能由索引
  该系统一旦文档已被保存到磁盘。如果
  文件尚未保存到磁盘,使用陈旧不会
  强制此过程。您可以使用观察操作来监控
  文件都保存在磁盘上和/或更新索引。

Irrespective of the stale parameter, documents can only be indexed by the system once the document has been persisted to disk. If the document has not been persisted to disk, use of the stale will not force this process. You can use the observe operation to monitor when documents are persisted to disk and/or updated in the index.

另外请注意,文档未立即添加到默认视图。阅读了解详情。

Also, note that documents are not immediately added to the view by default. Read this for more information.

这篇关于通过时间戳和用户id筛选职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆