1

I was able to do an $addToSet operation like this:

return Calls.update({ _id: callId }, {
    $addToSet: {
      units: {
        unit: callSign,
        currentStatus: "AS",
        statusColor: "yellow",
        textColor: "black",
        assignable: false,
        assignedBy: Meteor.users.findOne(this.userId).username,
        assignedOn: new Date(),
      },
    }
  }
);

But, when I try to update this collection like this:

return Calls.update({ _id: callId, "units.unit": callSign }, {
    $set: {
      "units.$.statusColor": "#74eeea",
      "units.$.textColor": "black",
      "units.$.currentStatus": "ER",
      "units.$.enrouteBy": Meteor.users.findOne(this.userId).username,
      "units.$.enrouteOn": new Date(),
    }
  }, {
    upsert: true
  }
);

I get this error:

Exception while invoking method 'call.enrouteUnit' MinimongoError: Key $ must not start with '$'

I know this is coming from the line (one if not all) in the $set where I'm using the "<array>.$.<key>: <value> format.

This has been working up till now, so not sure what's going on.

Update: The issue appears to be with my use of upsert. Not sure why it suddenly decided that was an issue, but seems to be. Continuing to look at it.

Styx
  • 9,863
  • 8
  • 43
  • 53
bmcgonag
  • 109
  • 2
  • 17
  • 4
    I think what's happening here is a new document is being added with a field starting **$**. It is stated in the documentation not to use the `$` positional operator with upsert. Check [this](https://docs.mongodb.com/manual/reference/operator/update/positional/#up._S_) out. – Hani Sep 29 '17 at 22:03
  • Just curious if you found the issue here. I'm experiencing a similar issue with a meteor update – bgmaster Oct 11 '17 at 20:38
  • Looks like @HannahMay is right. It is an issue with using upset with the $ as a positional operator. I've had to re-write to first see if the document / sub-document exists, and if so, update it. If not, then I have to us an update and insert the new sub-document instead. – bmcgonag Oct 12 '17 at 21:10

0 Answers0