Handling MongoDB's ISODate() when attempting to parse a serialized JSON string

前端 未结 6 1797
庸人自扰
庸人自扰 2021-02-07 11:00

I\'m using MongoDB via the official C# driver with an ASP.NET MVC web site.

I have the following C# model:

public class Contact
{
    public ObjectId Id          


        
6条回答
  •  悲&欢浪女
    2021-02-07 11:13

    as @Louie Almeda suggests, i think the best approach is with the BsonTypeMapper.MapToDotNetValue. BsonTypeMapperOptions can be customized too.

    Here's what i'm using to convert a list on BsonDocument to a json string using LINQ and newtonsoft serializer :

    var jsonString = JsonConvert.SerializeObject(bsonDocuments.ConvertAll(d => BsonTypeMapper.MapToDotNetValue(d)), Formatting.Indented)
    

提交回复
热议问题