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

时间:2013-09-16 11:55:22

标签: c# mongodb serialization

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

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

我没有注册任何课程地图。

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。我做了两件事。我可能只需要做第二次。

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

    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;
            }
        }
    }