按时间戳和用户 ID 过滤帖子 [英] Filtering posts by timestamp and userId

查看:37
本文介绍了按时间戳和用户 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

我做到了:

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

并且我在 URL 中将 userIds 作为多个键传递,但我得到旧的结果

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

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天全站免登陆