user3413838
user3413838

Reputation: 320

How do I update an array on a nested subdocument in Meteor?

I have a collection like this (called project):

{
  "_id" : id,
  name: '',
  title: '',
  "user": [
    {
      "username" : "manager",
      "role" : [
        "manager"
      ],
      "group" : "group",
      "Assign" : [
        "education"
      ]
    },
  ],
  "category" : [
    "education"
  ]
}

I'm trying to update the collection to replace the "Assign" array items:

Project.update({
  _id: id,
  'user.Assign': cat
}, {
  $set: {
    'user.$.Assign.$': new Cat
  }
}, {
  multi: true
})

I get the

Cannot apply the positional operator without a corresponding query field containing an array

error though. What is the proper way to do this in MongoDB?

Upvotes: 0

Views: 200

Answers (1)

bigmadwolf
bigmadwolf

Reputation: 3519

It doesn't look like you need that last positional operator. You may want to use something like $push on that simple array. Try changing the $set in your update statement to something like the following:

$push: {
    'user.$.Assign' : "new Cat"
}

Of course, you could use other array operators here if appropriate, such as $addToSet, etc.

Upvotes: 2

Related Questions