简体   繁体   English

使用C#MongoDB驱动程序,如何序列化对象引用的集合?

[英]Using C# MongoDB Driver, how to serialize a collection of object referements?

I have two classes, example: 我有两个类,例如:

public class A
{
    public string Id { get; set; }
    public string Name { get; set; }
    // Other properties...
}

public class B
{
    public string Id { get; set; }
    public ICollection<A> ReferredAObjects { get; set; }
    // Other properties...
}

I have create class maps with BsonClassMap.RegisterClassMap() for both A and B because they are stored separated into relative collections. 我已经为A和B创建了带有BsonClassMap.RegisterClassMap()的类映射,因为它们被分开存储为相对集合。

The problem starts when I try to map B, because I need to map the collection of A as references to external documents with some additional informations, so in this case I need to map only ids and names. 当我尝试映射B时问题就开始了,因为我需要将A的集合映射为带有一些额外信息的外部文档的引用,所以在这种情况下我只需要映射id和名称。

How can I create a class map for B that use a different mapping for A only inside it? 如何为B创建一个类映射,仅在其中使用不同的映射?

BsonClassMap is not your solution, you should write your custom IBsonSerializer for B class I just implemeted the Serialize method, the Deserilze works same way. BsonClassMap不是你的解决方案,你应该为B类我编写自定义IBsonSerializer我刚刚实现了Serialize方法, Deserilze工作方式相同。

public class BSerialzer : IBsonSerializer
{
    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
        throw new NotImplementedException();
    }

    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        throw new NotImplementedException();
    }

    public IBsonSerializationOptions GetDefaultSerializationOptions()
    {
        throw new NotImplementedException();
    }

    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        var b = (B)value;
        bsonWriter.WriteStartDocument();

        bsonWriter.WriteString("_id", b.Id);            
        bsonWriter.WriteStartArray("refs");
        foreach (var refobj in b.ReferredAObjects)
        {
            bsonWriter.WriteString(refobj.Id);
        }
        bsonWriter.WriteEndArray();            

        bsonWriter.WriteEndDocument();
    }
}

for a sample below objects 对于对象下面的示例

var a0 = new A
{
    Id = "0",
    Name = "0",
};

var a1 = new A
{
    Id = "1",
    Name = "1",
};

var b = new B
{
    Id = "b0",
    ReferredAObjects = new Collection<A> { a0, a1 }
};

collection.Insert(b);

will produce output like: 将产生如下输出:

{
    "_id" : "b0",
    "refs" : [ 
        "0", 
        "1"
    ]
}

Just remember to register this Sterilizer at program start-up: 只需记住在程序启动时注册此Sterilizer

BsonSerializer.RegisterSerializer(typeof(B), new BSerialzer());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB C#驱动程序-序列化集合到接口 - MongoDB C# driver - Serialize collection to interface 如何使用 C# 驱动程序检查 MongoDB 中是否存在集合? - How to check if collection exists in MongoDB using C# driver? 如何使用c#2.0驱动程序将数据插入mongodb集合? - How to insert data into a mongodb collection using the c# 2.0 driver? 如何使用MongoDB和C#驱动程序更新子文档集合 - How to update a sub document collection using MongoDB and C# driver 如何使用 mongodb C# 驱动程序获取子文档的集合? - How to fetch collection of subdocument using mongodb C# driver? 使用mongoDB c#驱动程序将对象序列化为字符串值(就像在其上调用ToString()) - Serialize object as string value (as if calling ToString() on it) with mongoDB c# driver MongoDB C#驱动程序:如何将对象列表序列化为对象ID列表? - MongoDB c# driver: How to serialize a list of objects as a list of object ids? 使用官方C#驱动程序序列化/反序列化MongoDB Bson文档 - Serialize/Deserialize MongoDB Bson document using Official C# driver 如何使用 c# mongodb 驱动程序获取 MongoDB 集合中所有键的名称? - How to get the names of all keys in a MongoDB collection using c# mongodb driver? 使用mongo C#驱动程序,如何序列化自定义对象数组以便存储它? - Using the mongo C# driver, how to serialize an array of custom object in order to store it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM