更新数组中的所有元素

时间:2014-08-30 11:43:01

标签: mongodb meteor

嘿我正在尝试使用以下代码更新数组中包含的所有子文档

  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

其中Users集合中的souls字段是数组。我收到以下错误消息

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

1 个答案:

答案 0 :(得分:0)

您不能使用位置运算符$来更新数组中的al子文档。请参阅the documentation:运算符匹配与查询匹配的数组的第一个元素。

这也意味着您的查询应该以某种方式使用数组字段(profile.character.souls)。你没有,因此错误。

此时没有您打算做的快捷方式,您需要手动列出所有索引。请参阅此问题:How to Update Multiple Array Elements in mongodb

相关问题