用mongoDB处理用户活动提要
Handling a user activity feed with mongoDB
我有一个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上的索引唯一)
相关文章:
- 活动在magento中记录用户角色和权限
- Facebook API过滤用户活动,只显示指定的好友
- 打开图形:Java脚本:必须使用活动访问令牌来查询有关当前用户的信息
- 如何在没有JQuery的情况下,通过对服务器的基本ajax调用,根据浏览器上的用户活动保持服务器会话活动
- 必须使用活动访问令牌来查询有关具有打开图的当前用户的信息
- 当没有用户活动时自动注销
- SignalR异常“;无法识别的用户标识.在活动SignalR连接期间,用户标识无法更改&”;
- 脸书:查询公共活动,无需用户登录
- 使用谷歌分析跟踪登录的用户活动
- 倒数计时器在任何用户活动(例如 onkeyup、鼠标移动等)上重置
- 检测浏览器选项卡是否处于活动状态或用户是否已切换
- FB.getLogin状态不起作用,如果用户处于非活动状态
- 如果 10 分钟后网页上没有用户活动,如何向上滚动到向下滚动页面
- 当用户单击子菜单并在新页面中打开时,子菜单将保持活动状态
- Javascript捕获用户活动
- 如何允许 Easeljs 代码在用户切换选项卡时保持活动状态
- 非活动用户功能
- 使用时间戳显示活动用户
- 活动用户时显示有角度的ng
- 如何在 30 分钟后注销非活动用户