简体   繁体   中英

Meteor MongoDB Document Update

I'm getting an an error 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 });

So far I tried:

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

and

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

What am I doing wrong? Can somebody help me please?

EDIT: My users collection looks like this:

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

I think that the way you are doing it, $set is an array, not an object. And as @chridam mentioned, you probably need to add a dot. Try this:

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

or directly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM