2

I use c# mongodb driver. When I want to update my specific values, it throws an exception. I used this before but I don't now how but i didn't get any error before. Here's my code:

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

And here's my Payment class:

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

1 Answer 1

6

You don't have Level property in your Payment class. If this is exactly what you want to do, then you need to add BsonIgnoreExtraElements attribute to your Payment class otherwise it throws an error as follows:

[BsonIgnoreExtraElements]
public class Payment
{
    public ObjectId Id { get; set; }
    public decimal Amount { get; set; }
    public Type Type { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

ow! i forgot that. thanks it worked. yes i am exactly want to do this. if it is greater than 100 i will add a property.
@sleven if your problem is solved, you could mark this answer as a solution :)
ah sorry i didn't know that. i tried now but it said you must wait 10 minutes for accept answer. maybe because of i'm new :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.