为什么MongoDB更新只影响单个文档

本文关键字:单个 文档 影响 MongoDB 更新 为什么 | 更新日期: 2023-09-27 18:26:37

我有一个具有以下"架构"的集合:

{
   "_id": { "$oid": "543cd94799c3ff7a2850a1b6" },
   "Type": 1,
   "Information": [
     {
       "Type" : 2,
       "Colors": [],
       "Heights": [],
       "Widths": []
     }
   ]
}

我有"颜色","高度","宽度"数组嵌套在数组"信息"中。

我正在尝试使用以下更新查询更新集合中的一些文档:

var query = Query.And(Query.Exists(Entity.INFORMATION + "." + Information.COLORS),
                  Query.Exists(Entity.INFORMATION + "." + Information.HEIGHTS),
                  Query.Exists(Entity.INFORMATION + "." + Information.WIDTHS), 
                  Query.EQ(Entity.TYPE, typeId),
                  Query.ElemMatch(Entity.INFORMATION, Query.EQ(Information.TYPE, informationTypeId)));
var update = MongoDB.Driver.Builders.Update.Set(Entity.INFORMATION + ".$." + Information.WIDTHS, new BsonArray(new Width[0]))
                                       .Set(Entity.INFORMATION + ".$." + Information.COLORS, new BsonArray(new Color[0]))
                                       .Set(Entity.INFORMATION + ".$." + Information.HEIGHTS, new BsonArray(new Height[0]))
                                       .Set(Entity.INFORMATION + ".$." + Information.TYPE, BsonNull.Value);
Collection.Update(query, update, UpdateFlags.Multi);

似乎只有第一份文件受到影响。其余部分不受影响。

如何修复此更新查询,使其也适用于其余文档?我使用了UpdateFlags.Multi,但没有运气。。

我想将信息类型设置为null,并清除信息数组中的嵌套数组。

为什么MongoDB更新只影响单个文档

好的,我已经将信息压平了,现在我只有一个级别的数组。

所以,现在正在发挥作用。

谢谢大家。