C# 如何将BsonDocument对象反序列化回类

C# 如何将BsonDocument对象反序列化回类,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,从服务器获取BsonDocument对象后,如何将其反序列化回类 QueryDocument _document = new QueryDocument("key", "value"); MongoCursor<BsonDocument> _documentsReturned = _collection.FindAs<BsonDocument>(_document); foreach (BsonDocument _document1 in _documentsRetur

从服务器获取BsonDocument对象后,如何将其反序列化回类

QueryDocument _document = new QueryDocument("key", "value");
MongoCursor<BsonDocument> _documentsReturned = _collection.FindAs<BsonDocument>(_document);

foreach (BsonDocument _document1 in _documentsReturned)
{
    //deserialize _document1
    //?
}
QueryDocument\u document=新的QueryDocument(“key”、“value”);
MongoCursor\u DocumentsReturn=\u collection.FindAs(\u document);
foreach(返回文件中的文件1)
{
//反序列化文档1
//?
}

我是否使用BsonReader进行反序列化?

实际上有三种方法:

1.在
FindAs

3.手动将bson文档映射到您的类:

var myClass = new Mytype();
myClass.Name = bsonDoc["name"].AsString;

在大多数情况下,您可以接受第一种方法。但是,有时,当您的文档是非结构化的时,您可能需要第三种方法。

对于大型应用程序和具有一些结构化数据的应用程序,建议在创建和获取数据时使用自定义模型,而不是使用BsonDocument

创建模型是反序列化的重要步骤

创建模型时要记住的有用注释:

  • 在模型中添加
    id
    属性。尝试使用
    [BsonId]
    属性获得良好实践:
  • 创建注释为
    [BsonExtraElements]
    的属性,该属性将用于保存反序列化过程中找到的任何额外元素
  • 您可以使用
    [bsonement]
    指定元素名称
  • [BsonIgnoreIfDefault]
    -初始化BsonIgnoreIfDefaultAttribute类的新实例
示例模型结构,我试图涵盖最大的案例。我为_id属性创建了一个基类,只是为了更好的架构,但您也可以直接在
MyModel
类中使用

    public abstract class BaseEntity
    {
        // if you'd like to delegate another property to map onto _id then you can decorate it with the BsonIdAttribute, like this
        [BsonId]
        public string id { get; set; }
    }

    public class MyModel: BaseEntity
    {
        [BsonElement("PopulationAsOn")]
        public DateTime? PopulationAsOn { get; set; }
    
        [BsonRepresentation(BsonType.String)]
        [BsonElement("CountryId")]
        public int CountryId { get; set; }

        [Required(AllowEmptyStrings = false)]
        [StringLength(5)]
        [BsonIgnoreIfDefault]
        public virtual string CountryCode { get; set; }

        [BsonIgnoreIfDefault]
        public IList<States> States { get; set; }

        [BsonExtraElements]
        public BsonDocument ExtraElements { get; set; }
    }

反序列化类Class1的Property1属性时出错:ReadString只能在CurrentBsonType为String时调用,而不能在CurrentBsonType为ObjectId时调用。我将bson对象ID放在类的guid中,并映射该类。如何修复此问题?@iefpw:Error表示数据库和类中的属性1类型不同(数据库中的objectId和类中的字符串)。我试图反序列化到错误的类。将“ObjectId{get;set;}添加到类中,基本上一切都可以在没有类映射的情况下工作。
var myClass = new Mytype();
myClass.Name = bsonDoc["name"].AsString;
    public abstract class BaseEntity
    {
        // if you'd like to delegate another property to map onto _id then you can decorate it with the BsonIdAttribute, like this
        [BsonId]
        public string id { get; set; }
    }

    public class MyModel: BaseEntity
    {
        [BsonElement("PopulationAsOn")]
        public DateTime? PopulationAsOn { get; set; }
    
        [BsonRepresentation(BsonType.String)]
        [BsonElement("CountryId")]
        public int CountryId { get; set; }

        [Required(AllowEmptyStrings = false)]
        [StringLength(5)]
        [BsonIgnoreIfDefault]
        public virtual string CountryCode { get; set; }

        [BsonIgnoreIfDefault]
        public IList<States> States { get; set; }

        [BsonExtraElements]
        public BsonDocument ExtraElements { get; set; }
    }
cursor = await _collection.FindAsync(filter, 
new FindOptions<MyModel, MyModel>() 
{ BatchSize = 1000, Sort = sort }, ct);