为什么我的MongoDb查询在更新时插入嵌入文档?

为什么我的MongoDb查询在更新时插入嵌入文档?,mongodb,mongodb-update,Mongodb,Mongodb Update,这是我的MongoDB查询: db.events.update({date:{$gte: ISODate("2014-09-01T00:00:00Z")}},{$set:{"artists.$.soundcloud_toggle":false}},{multi:true,upsert:false}) 显然,我无法使用“艺术家.$.soundcloud\u切换”来更新艺术家阵列中的所有艺术家文档: “$运算符可以更新匹配的第一个数组元素 使用$elemMatch()运算符指定的多个查询条件。 "

这是我的MongoDB查询:

db.events.update({date:{$gte: ISODate("2014-09-01T00:00:00Z")}},{$set:{"artists.$.soundcloud_toggle":false}},{multi:true,upsert:false})
显然,我无法使用“艺术家.$.soundcloud\u切换”来更新艺术家阵列中的所有艺术家文档:

“$运算符可以更新匹配的第一个数组元素 使用$elemMatch()运算符指定的多个查询条件。 "

我很乐意多次运行查询,更改数组的索引,以便在与查询匹配的每个事件中设置每个艺术家的soundcloud_toggle属性,例如

artists.0.soundcloud_toggle
artists.1.soundcloud_toggle
artists.2.soundcloud_toggle
artists.3.soundcloud_toggle
问题是:如果在艺术家数组中只有一个艺术家文档,并且我使用“artists.1.soundcloud\u toggle”运行查询,它将使用单个属性将艺术家文档插入艺术家数组:

{
   "soundcloud_toggle" : true
},
(我已经声明了“upsert:false”,默认情况下它应该是false)


如果没有现有文档,如何阻止查询插入文档并设置soundcloud\u toggle:false?我只希望它在给定的艺术家数组索引中存在艺术家时更新属性。

如您所说,如果您不介意通过多个查询完成操作,则可以将
$exists
条件添加到过滤器中

例如,在第5次迭代中,当更新index=4时,添加:
“artists.4”:{$exists:true}
,如:

db.events.update(
    { date: {$gte: ISODate("2014-09-01T00:00:00Z")},
      "artists.4": {$exists: true} },
    { $set:{ "artists.4.soundcloud_toggle" :false } },
    { multi: true, upsert: false }
)