gpt4 book ai didi

c# - mongodb c#更新嵌入式文档

转载 作者:行者123 更新时间:2023-11-30 18:46:01 24 4
gpt4 key购买 nike

如何更新一些嵌入文档,例如:

我有对象:

{ 
"_id" : ObjectId("5648c70f574b7e15b0a2ddda"),
"Username" : "admin@yourStore.com",
"Email" : "admin@yourStore.com",
"ShoppingCartItems" : [
{
"ProductId" : NumberInt(4),
"AdditionalShippingChargeProduct" : NumberInt(0),
},
{
"ProductId" : NumberInt(4),
"AdditionalShippingChargeProduct" : NumberInt(0),
},
{
"ProductId" : NumberInt(5),
"AdditionalShippingChargeProduct" : NumberInt(0),
},
],
}

我需要更新字段 AdditionalShippingChargeProduct = 5 但仅限于 ProductId 等于 4 的地方。

这段代码只更新第一个位置,我需要更新所有匹配的位置。

    var builder = Builders<Customer>.Filter;
var filter = builder.ElemMatch(x => x.ShoppingCartItems, y => y.ProductId == 4);
var update = Builders<Customer>.Update
.Set("ShoppingCartItems.$.AdditionalShippingChargeProduct", 5);

var result = customer.UpdateManyAsync(filter, update).Result;

感谢您的帮助。

最佳答案

{ShoppingCartItems.ProductId:4}, {$set:{"ShoppingCartItems.$.AdditionalShippingChargeProduct":6 }}

关于c# - mongodb c#更新嵌入式文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33834416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com