0

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

我已经实现了 IBsonSerializer,并且我的数据以我想要的方式显示在数据库中。但是,在我插入集合之后,我的 ObjectId 现在为空(000000000000000000000000),之前它的 ObjectId 为 5236ea9949444a06d7f50012。我可以看到 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()
        );

而且我没有注册任何班级地图。

4

1 回答 1

0

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

  1. 实施 IBsonIdProvider - 这有 GetDocumentId 和 SetDocumentId - 不确定我需要这样做。
  2. 在我的序列化方法中,用元素名称“_id”序列化 objectid - 绝对需要这样做。

这是我的代码:

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;
        }
    }
}
于 2013-09-17T01:00:50.617 回答