在 Mongo 自定义序列化中,如何获取对象 ID

本文关键字:何获 取对象 ID Mongo 自定义 序列化 | 更新日期: 2023-09-27 18:31:24

在Mongo自定义序列化中,如何获取objectId?

我已经实现了IBsonSerializer,我的数据以我想要的方式显示在数据库中。但是,在我插入集合后,我的 ObjectId 现在是空的 (0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000我可以看到 ObjectId 在数据库中,所以我知道它在那里,只是在我插入集合后它没有填充到我的 C# 对象中。

这是我的代码:

public class EvaluationResultMongoSerializer : IBsonSerializer
{
    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        throw new NotImplementedException();
    }
    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
        throw new NotImplementedException();
    }
    public IBsonSerializationOptions GetDefaultSerializationOptions()
    {
        return null;
    }
    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        EvaluationResult er = (EvaluationResult)value;
        bsonWriter.WriteStartDocument();
        bsonWriter.WriteDouble("Duration", er.DurationInSeconds);
        bsonWriter.WriteDateTime("Start", BsonUtils.ToMillisecondsSinceEpoch(er.startTime));
        bsonWriter.WriteDateTime("End", BsonUtils.ToMillisecondsSinceEpoch(er.endTime));
        bsonWriter.WriteString("Result", er.Result.ValueAsString);
        bsonWriter.WriteEndDocument();
    }
}

我像这样注册映射:

BsonSerializer.RegisterSerializer(
            typeof(EvaluationResult),
            new EvaluationResultMongoSerializer()
        );

而且我没有注册任何类映射。

在 Mongo 自定义序列化中,如何获取对象 ID

我让它工作了。我做了两件事。我可能只需要做第二个。

  1. 实现IBsonIdProvider - 它有GetDocumentId和SetDocumentId - 不确定我需要这样做。
  2. 在我的序列化方法中,使用元素名称"_id"序列化对象标识 - 绝对需要这样做。

这是我的代码:

public class EvaluationResultMongoSerializer : IBsonSerializer, IBsonIdProvider 
{
    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        throw new NotImplementedException();
    }
    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
        EvaluationResult er = new EvaluationResult();
        bsonReader.ReadStartDocument();
        er.Id = bsonReader.ReadObjectId();
        er.DurationInSeconds = bsonReader.ReadDouble();
        er.startTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
        er.endTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
        er.Result = EvaluationStatus.Parse(bsonReader.ReadString());
        er.JSON = bsonReader.ReadString();
        bsonReader.ReadEndDocument();
        return er;
    }
    public IBsonSerializationOptions GetDefaultSerializationOptions()
    {
        return null;
    }
    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        EvaluationResult er = (EvaluationResult)value;
        bsonWriter.WriteStartDocument();
        bsonWriter.WriteObjectId("_id", er.Id);
        bsonWriter.WriteDouble("Duration", er.DurationInSeconds);
        bsonWriter.WriteDateTime("Start", BsonUtils.ToMillisecondsSinceEpoch(er.startTime));
        bsonWriter.WriteDateTime("End", BsonUtils.ToMillisecondsSinceEpoch(er.endTime));
        bsonWriter.WriteString("Result", er.Result.ValueAsString);
        bsonWriter.WriteString("JSON", er.JSON);
        bsonWriter.WriteEndDocument();
    }
    public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator)
    {
        idNominalType = typeof(ObjectId);
        idGenerator = ObjectIdGenerator.Instance;
        var mongoDocument = document as EvaluationResult;
        if (mongoDocument == null)
        {
            id = null;
            return false;
        }
        id = mongoDocument.Id;
        return true;
    }
    public void SetDocumentId(object document, object id)
    {
        var mongoDocument = document as EvaluationResult;
        if (mongoDocument != null)
        {
            mongoDocument.Id = (ObjectId)id;
        }
    }
}