【问题标题】:C# MongoDB UpdateManyAsync with Set method throws an exception带有 Set 方法的 C# MongoDB UpdateManyAsync 引发异常
【发布时间】:2017-01-26 01:25:40
【问题描述】:

我使用 c# mongodb 驱动程序。当我想更新我的特定值时,它会引发异常。我以前用过这个,但我现在不知道,但我以前没有收到任何错误。这是我的代码:

var result = await col.UpdateManyAsync(
      p => p.X > 5, 
      Builders<Payment>.Filter.Gt(p => p.Amount, 100).Set("Level", "High")
);

这是我的支付类:

public class Payment
    {
        public ObjectId Id { get; set; }
        public decimal Amount { get; set; }
        public Type Type { get; set; }
    }

【问题讨论】:

    标签: c# mongodb mongodb-.net-driver


    【解决方案1】:

    您的 Payment 类中没有 Level 属性。如果这正是您想要做的,那么您需要将 BsonIgnoreExtraElements 属性添加到您的 Payment 类,否则它会引发如下错误:

    [BsonIgnoreExtraElements]
    public class Payment
    {
        public ObjectId Id { get; set; }
        public decimal Amount { get; set; }
        public Type Type { get; set; }
    }
    

    【讨论】:

    • 哇!我忘记了。谢谢它的工作。是的,我正是想这样做。如果大于 100,我将添加一个属性。
    • @sleven 如果您的问题已解决,您可以将此答案标记为解决方案 :)
    • 对不起,我不知道。我现在试过了,但它说你必须等待 10 分钟才能接受答案。也许是因为我是新人:)
    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多