用mongoDB处理用户活动提要

Handling a user activity feed with mongoDB

本文关键字:活动 用户 mongoDB 处理      更新时间:2023-09-26

我有一个activityFeeds集合,其中每个文档的主键与它引用的用户的_id相同。

我做了一个getTimeKey函数,根据特定的日期返回一个字符串

/*
** Return a string like 1_2015 for january 2015
** If timestamp is not defined, current date is used
*/
getTimeKey = function (timestamp)
{
    var date = (timestamp === undefined ? new Date() : new Date(timestamp));
    return date.getMonth().toString() + "_" + date.getYear().toString();
}

当我在JS中使用它来访问对象属性时,它工作得很好:

myProperty = myObj[getTimeKey()]; // get the aray of activity for the current month

问题是当我想添加一个活动到我的文档/集合,getTimeKey()被解释为一个字符串在mongo查询。

/*
** Add a new activity to a user activity feed
** Get the time key from the passed date
** Returns false if a required parameter is not defined
** If the user has already an activity feed started, then check if the time key exists
** If it exists, push it the new activity, else create it
** If the user do not already have an activity feed started, then create it
**
** PARAMETERS :
**      uid     -> user's activity feed to update
**      type    -> the type of activity, see below
**      data    -> some specific data, see below
**      date    -> (optionnal) the date of the event
**
** TYPES :
**      "ntt"   -> data: { tId: threadId }
**      "reply" -> data: { tId: threadId, pId: postId }
**      "mate"  -> data: { mId: mateId }
*/
addNewActivityTo = function (uid, type, data, date)
{
  if (uid && type && data && timeKey)
  {
    var feed = ActivityFeeds.findOne({_id: uid}, {fields: {getTimeKey(date): 1}});
    if (feed)
    {
      if (feed[getTimeKey(date)])
      {
        ActivityFeeds.update({_id: uid}, {
          $push: { getTimeKey(date): { type: type, data: data, date: date } }
        }, {multi: false});
      }
      else
      {
        ActivityFeeds.update({_id: uid}, {
          $set: { getTimeKey(date): [ { type: type, data: data, date: date } ] }
        }, {multi: false});
      }
    }
    else
    {
      ActivityFeeds.insert({
        _id: uid,
        getTimeKey(date): [ { type: type, data: data, date: date } ]
      });
      return { type: type, data: data, date: date };
    }
  }
  devError("Couldn't add new activity to user, parameters are missing.");
  return false;
}

我想要的是能够在查询中创建所需的键(例如,9月的7_2014)。2014年)或寻找它。我怎样才能使mongo明白,他必须寻找getTimeKey()的返回,而不是把它作为一个字符串?

如果我存储getTimeKey()返回如下:var timeKey = getTimeKey(date);,并将每个getTimeKey(日期)替换为timeKey,它可以将其插入数据库中,但属性键将是"timeKey"。因为它目前是,它抛出我一个流星构建错误,因为意外的括号。

有人遇到过这个问题吗?

在"Object Notation"中使用时,javascript将"key"一侧的所有内容"字符串化"。因此,使用"数组表示法"从代码中动态设置键:

var query = {_id: uid},
    projection = { "fields": {} };
projection["fields"][getTimeKey(date)] = 1;
var feed = ActivityFeeds.findOne(query, projection);

使"projection"对象在本例中看起来像:

{ "fields": { "7_2014": 1 } }

下面是一个如何使用MongoDB构建活动提要的示例:https://github.com/GetStream/mongodb-activity-feed

MongoDB活动feed项目使用5种不同的模式:
  • Activity:该Activity的实际数据
  • Feed:操作日志。这存储了应该将某个活动添加到某个提要。(饲料、时间和操作时间的索引)
  • Feed Group:一组Feed,例如:用户、通知、时间轴等(名称唯一)
  • Feed:一个特定的Feed,例如用户:scott,通知:josh,时间轴:tom等。(组和fedid唯一)
  • Follow:两个feed之间的Follow关系。(源、目标、目标desc上的索引唯一)