c# mongodb 文档是否应该继承自 BsonDocument

Should c# mongodb documents inherit from BsonDocument

用 c# 编写的 mongodb 文档 classes 应该继承 BsonDocument 吗?

例如:

/// <summary>
/// Company document
/// </summary>
public class CompanyDocument : BsonDocument
{
    /// <summary>
    /// Collection name
    /// </summary>
    public const string COLLECTION_NAME = "company";

    /// <summary>
    /// Unique company name
    /// </summary>
    [BsonElement("name")]
    public string Name
    {
        get;
        set;
    }

    /// <summary>
    /// List of referenced users
    /// </summary>
    [BsonElement("users")]
    public IList<MongoDBRef> Users
    {
        get;
        set;
    }
}

因为想query数据的时候,好像需要继承BsonDocument。或者它应该更像一个 POCO 对象还是从其他东西继承?

非常感谢!

我知道这是一个老问题,但以防万一有人像我一样偶然在这里寻找答案。答案是否定的。

虽然这是可能的,但已注意到它会导致许多问题。专门看到的那个是带连载的

我 运行 跨越这两个错误报告:

他们的一些摘录比我能更好地解释它:

If you really want to subclass BsonDocument what you will need to do is wire things up so that your MyBsonDocument values get serialized correctly. -- Robert Stam


BsonDocument can represent any structure that exists. So, the only reason to inherit is (a) to provide a different behavior (RawBsonDocument, LazyBsonDocument), or (b) to provide strongly-typed access to certain fields. -- Craig Wilson