在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()
);
我没有注册任何课程地图。
答案 0 :(得分:0)
我得到了它的工作。我做了两件事。我可能只需要做第二次。
这是我的代码:
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;
}
}
}