我有一个activityFeeds集合,其中每个文档的主键与它引用的用户的_id是相同的_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;
}


我想要的是能够在查询中创建想要的键(例如,2014年9月为7_2014)或进行搜索。我如何使mongo理解他必须寻找getTimeKey()的返回值,而不要将其作为字符串?

如果我存储getTimeKey()这样的返回:var timeKey = getTimeKey(date);,并用timeKey替换每个getTimeKey(date),它可以将其插入数据库中,但是属性键将为"timeKey"。就目前而言,由于出乎意料的括号,它引发了Meteor构建错误。

有人遇到过这个问题吗?

最佳答案

当在“对象表示法”中使用JavaSript时,将“键”侧的所有内容“字符串化”。因此,请使用“数组表示法”从代码中动态设置键:

var query = {_id: uid},
    projection = { "fields": {} };

projection["fields"][getTimeKey(date)] = 1;

var feed = ActivityFeeds.findOne(query, projection);


这使得“投影”对象在这种情况下看起来像:

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


这是您追求的结果。

09-25 17:39