【问题标题】:Meteor MongoDB Document UpdateMeteor MongoDB 文档更新
【发布时间】:2016-10-07 19:26:11
【问题描述】:

我收到一个错误MongoError: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.

const modifier = { $set: {}, $inc: { 'hearts.counter': 1 } };
modifier.$set[`hearts.records${i}.expDate`] = expDate;
Meteor.users.update(lookUpUser._id, { modifier });

到目前为止我尝试过:

Meteor.users.update(lookUpUser._id, modifier);

const modifier = { $set: {}, $inc: {} };
modifier.$inc['hearts.counter'] = 1;

我做错了什么?有人可以帮帮我吗?

编辑: 我的用户集合如下所示:

{
  "_id": "xxxxx",
  "username": "xxxx",
  "hearts": {
    "counter": 0,
    "records": [{
      "owner": "xxxxx",
      "expDate": Date
    }, {
      "owner": "xxxxx",
      "expDate": Date
    }]
  }
}

【问题讨论】:

  • 是的,我正在循环遍历文档的数组 hearts.records,如果 hearts.records[index].expDatenew Date() 多 30 天,则将 $inc hearts.counter 更新为 1,将 $set hearts.records[index].expDate 更新为新的 expDate。我希望这很清楚
  • 尝试在另一个属性中添加一个点hearts.records.${i}.expDate
  • 我刚刚试了一下。仍然出现同样的错误

标签: mongodb meteor


【解决方案1】:

我认为你这样做的方式,$set 是一个数组,而不是一个对象。正如@chridam 提到的,您可能需要添加一个点。试试这个:

const modifier = { $set: {}, $inc: { 'hearts.counter': 1 } };
modifier.$set = { [`hearts.records.${i}.expDate`]: expDate }
Meteor.users.update(lookUpUser._id, modifier);

或直接:

const modifier = {
  $set: {
    [`hearts.records.${i}.expDate`]: expDate,
  },
  $inc: {
    'hearts.counter': 1,
  },
};
Meteor.users.update(lookUpUser._id, modifier);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2020-09-26
    • 2016-08-04
    • 2014-02-05
    相关资源
    最近更新 更多