C# 如何在MongoDB中清除集合中的嵌套数组?
我使用MongoDB C#驱动程序,我想知道如何通过使用更新查询清除MongoDB中的嵌套数组 以下是我的收藏,其中包含以下“模式”下的一些文档: 我想要的是清除(删除所有元素)嵌套数组的颜色、高度和宽度 我试过这样的方法:C# 如何在MongoDB中清除集合中的嵌套数组?,c#,mongodb,collections,updates,C#,Mongodb,Collections,Updates,我使用MongoDB C#驱动程序,我想知道如何通过使用更新查询清除MongoDB中的嵌套数组 以下是我的收藏,其中包含以下“模式”下的一些文档: 我想要的是清除(删除所有元素)嵌套数组的颜色、高度和宽度 我试过这样的方法: var query = Query.And(Query.Exists(Entity.INFORMATION + "." + Information.COLORS), Query.Exists(Entity.INFORMATION
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);
但这似乎不起作用
请帮忙
我尝试了以下本机查询,但似乎不起作用:
db.myCollection.update(
{
"Information.Colors": {
$exists: true
},
"Type": 1
},
{
$set: {
"Information.$.Colors": [],
"Information.$.Widths": [],
"Information.$.Heights": [],
"Information.$.Type" : null
}
},
false, true)
它仅适用于集合中的第一个文档
其余的都不受影响…:($对于嵌套数组是必需的,并且还需要具有更新字段访问权限的查找查询
var update = Update
.Set("Information.$.Heights", new BsonArray(new int[0]))
.Set("Information.$.Colors", new BsonArray(new int[0]))
.Set("Information.$.Widths", new BsonArray(new int[0]));
var q = Query.Exists("Information.Heights"); // important
c.Update(q, update, UpdateFlags.Multi);
无法使用位置运算符更新数组中的所有文档
请参阅:是否有人可以帮助我?更新变量的创建是正确的?有些人使用“信息.$.Colors”等。有什么区别?“信息”不是一个json对象,而是一个json数组。我认为当它是嵌套在另一个数组中的数组时会更棘手。有什么建议吗?这似乎是可行的,但它只影响集合中的一个文档。其余文档不受影响。此外,我使用的UpdateFlags.Multi与上述范例完全相同。我已经更新了我在中使用的代码但是,我真的无法更新集合中的所有文档。只更新了第一个文档。
var update = Update
.Set("Information.$.Heights", new BsonArray(new int[0]))
.Set("Information.$.Colors", new BsonArray(new int[0]))
.Set("Information.$.Widths", new BsonArray(new int[0]));
var q = Query.Exists("Information.Heights"); // important
c.Update(q, update, UpdateFlags.Multi);