0

Hey I'm trying to update all subdocuments contained within an array using the following code

  setDuelToInactive: (duel) ->
    Duels.update({_id: duel._id}, $set: {active: false})
    userOneId = duel.userOneId
    userTwoId = duel.userTwoId

    Users.update({_id: userOneId}, { $set: { 'profile.character.souls.$.active': false} })
    Users.update({_id: userTwoId}, { $set: { 'profile.character.souls.$.active': false} })


    return

where the souls field in the Users collection is the array. I receive the following error message

MongoError: Cannot apply the positional operator without a corresponding query field containing an array.
Tarlen
  • 3,657
  • 8
  • 30
  • 54
  • The MongoDB server (as at 2.6) does not support updating all subdocuments in an array using the positional operator ($). The positional operator only applies to the first matching element. There is a feature request you can watch/upvote in the MongoDB issue tracker: [SERVER-1243: Use positional operator to update all items in an array](https://jira.mongodb.org/browse/SERVER-1243). The fastest interim workaround would probably be to retrieve the document with the full array and do the required manipulation in your application code before saving. – Stennie Aug 30 '14 at 13:00

1 Answers1

0

You cannot use the positional operator $ to update al subdocuments inside an array. See the documentation: the operator matches the first element of the array that matches the query.

This also means that your query should use the array field (profile.character.souls) somehow. Yours do not, hence the error.

At this moment there is no shortcut for what you intend to do, you need to list all indexes manually. See this question: How to Update Multiple Array Elements in mongodb

Community
  • 1
  • 1
Hubert OG
  • 19,314
  • 7
  • 45
  • 73