C# 为什么MongoDB更新只影响单个文档?
我有一个具有以下“模式”的集合: 我在数组“信息”中嵌套了“颜色”、“高度”、“宽度”数组 我正在尝试使用以下更新查询更新集合中的某些文档:C# 为什么MongoDB更新只影响单个文档?,c#,mongodb,collections,updates,mongodb-.net-driver,C#,Mongodb,Collections,Updates,Mongodb .net Driver,我有一个具有以下“模式”的集合: 我在数组“信息”中嵌套了“颜色”、“高度”、“宽度”数组 我正在尝试使用以下更新查询更新集合中的某些文档: var query = Query.And(Query.Exists(Entity.INFORMATION + "." + Information.COLORS), Query.Exists(Entity.INFORMATION + "." + Information.HEIGHTS),
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,但没有运气
我想将信息类型设置为空,并清除信息数组中的嵌套数组。好的,我已将信息展平,现在只有一级数组 所以,现在是工作
谢谢大家。有人能在这方面真正帮助我吗?这看起来很正确。您可以尝试在shell中执行此查询并查看它是否涉及多个文档吗?我将在家中尝试并告知您。它不起作用。还有其他帮助吗?
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);