How to use decimal type in MongoDB

前端 未结 4 698
终归单人心
终归单人心 2020-11-29 11:07

How can I store decimals in MongoDB using the standard C# driver? It seems that all decimals are stored inside the database as strings.

4条回答
  •  粉色の甜心
    2020-11-29 11:39

    I solved it like Erik Kindling, by annotating the object like this:

    [BsonRepresentation(BsonType.Decimal128)]
    public decimal Price {get; set;}
    

    However, it is important that you re-populate your mongodb collection otherwise the string value will be preserved. The field value will be in this format once inserted properly:

    "Price" : NumberDecimal("99")
    

提交回复
热议问题