MongoDB C#驱动程序 - 更新嵌入式阵列的特定成员 [英] MongoDB C# Driver - Updating a specific member of an embedded array

查看:68
本文介绍了MongoDB C#驱动程序 - 更新嵌入式阵列的特定成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MongoDB文档集合,它有一个嵌入的文档集合作为数组。如果数组成员文档中存在某些值,我想查找并更新该数组的特定成员。



我可以找到没有问题的数组成员(查询文档完美的工作),但是当我尝试用新的/更新的值设置成员时,第一个成员总是被设置,而不是我找到的成员。



如何更新我找到的相同数组成员,而不是数组中的第一项。这是我目前正在做的事情,其中​​'InventoryData'是嵌入式数组的名称,而InventoryRecord是更新的成员,其中包含我的查询成功匹配的值:



以下是成功找到匹配数组成员的查询:

I have a MongoDB Collection of Documents that has an imbedded collection of documents as an array. I want to find and update a specific member of that array if certain values exist in the array member document.

I can find the array member without issue(the Query Document works perfectly), but when I try to $set the member with the new/updated values, the first member is always set, rather than the member that I found.

How can I update the same array member that I found, rather than the first item in the array. Here is what I am currently doing, where 'InventoryData' is the name of the embedded array, and the InventoryRecord is the updated member that contains the value my query is matching on successfully:

Here is the query that successfully finds the matching array member:

query = new QueryDocument { { "_id", BTKey }, { "InventoryData.WarehouseCode", InventoryRecord.WarehouseCode }, { "InventoryData.InventoryType", InventoryRecord.InventoryType }, { "InventoryData.CustomerNumber", InventoryRecord.CustomerNumber }



然后,这是我尝试使用更新该成员的地方。$:




Then, here is where I try to update that member using .$ :

var update2 = MongoDB.Driver.Builders.Update.Set("InventoryData.$", InventoryRecord.ToBsonDocument());

MongoConnection.Products.Update(query, update2, UpdateFlags.None);



...但数组中的第一项始终是更新,而不是目标项目。



任何帮助将不胜感激,谢谢。


...but the first item in the array is always updated, rather than the target item.

Any help will be greatly appreciated, thanks.

推荐答案

使用新的/更新的值设置成员,始终设置第一个成员,而不是我找到的成员。



如何更新相同的数组成员我找到了,而不是数组中的第一个项目。这是我目前正在做的事情,其中​​'InventoryData'是嵌入式数组的名称,而InventoryRecord是更新的成员,其中包含我的查询成功匹配的值:



以下是成功找到匹配数组成员的查询:

set the member with the new/updated values, the first member is always set, rather than the member that I found.

How can I update the same array member that I found, rather than the first item in the array. Here is what I am currently doing, where 'InventoryData' is the name of the embedded array, and the InventoryRecord is the updated member that contains the value my query is matching on successfully:

Here is the query that successfully finds the matching array member:
query = new QueryDocument { { "_id", BTKey }, { "InventoryData.WarehouseCode", InventoryRecord.WarehouseCode }, { "InventoryData.InventoryType", InventoryRecord.InventoryType }, { "InventoryData.CustomerNumber", InventoryRecord.CustomerNumber }



然后,这里是我尝试使用更新该成员的地方。


Then, here is where I try to update that member using .






:

var update2 = MongoDB.Driver.Builders.Update.Set("InventoryData.


,InventoryRecord.ToBsonDocument());

MongoConnection.Products.Update(query,update2,UpdateFlags.None);
", InventoryRecord.ToBsonDocument()); MongoConnection.Products.Update(query, update2, UpdateFlags.None);



...但数组中的第一项始终是更新,而不是目标项目。



任何帮助将不胜感激,谢谢。


...but the first item in the array is always updated, rather than the target item.

Any help will be greatly appreciated, thanks.


这篇关于MongoDB C#驱动程序 - 更新嵌入式阵列的特定成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆