任何人都可以帮助我理解为什么我得到以下消息:“ System.NullReferenceException:'对象引用未设置为对象的实例。在方法的最后一行。我的目标是在要查询的同一集合中建立自我查找。我认为这与驱动程序无法正确序列化模型有关,但是我无法理解为什么会发生这种情况。
public void CanGetAggregatedComponents()
{
PipelineStageDefinition<ComponentRecordDataModel, AggregatedAlignedComponentRecordDataModel> lookup;
lookup = PipelineStageDefinitionBuilder.Lookup<ComponentRecordDataModel,
ComponentRecordDataModel,
AggregatedAlignedComponentRecordDataModel>(
_mongoContext.Collection,
local => local.RecordAlignments,
foreign => foreign.Id,
(AggregatedAlignedComponentRecordDataModel lookupResult) =>lookupResult
);
// var expectedLookup =
// @"{$lookup: { from: 'componentRecord', localField: 'recordAlignments', foreignField: '_id', as: 'alignedToRecord' } }";
var rendered = lookup.Render(BsonSerializer.SerializerRegistry.GetSerializer<ComponentRecordDataModel>(),
BsonSerializer.SerializerRegistry);
}
答案 0 :(得分:0)
您的查询可能为空。试试这个:
public void CanGetAggregatedComponents()
{
PipelineStageDefinition<ComponentRecordDataModel, AggregatedAlignedComponentRecordDataModel> lookup;
lookup = PipelineStageDefinitionBuilder.Lookup<ComponentRecordDataModel,
ComponentRecordDataModel,
AggregatedAlignedComponentRecordDataModel>(
_mongoContext.Collection,
local => local.RecordAlignments,
foreign => foreign.Id,
(AggregatedAlignedComponentRecordDataModel lookupResult) =>lookupResult
);
// var expectedLookup =
// @"{$lookup: { from: 'componentRecord', localField: 'recordAlignments', foreignField: '_id', as: 'alignedToRecord' } }";
if(lookup !=null)
{
var rendered = lookup.Render(BsonSerializer.SerializerRegistry.GetSerializer<ComponentRecordDataModel >(),BsonSerializer.SerializerRegistry);
}
}
或这个
public void CanGetAggregatedComponents()
{
PipelineStageDefinition<ComponentRecordDataModel, AggregatedAlignedComponentRecordDataModel> lookup;
lookup = PipelineStageDefinitionBuilder.Lookup<ComponentRecordDataModel,
ComponentRecordDataModel,
AggregatedAlignedComponentRecordDataModel>(
_mongoContext.Collection,
local => local.RecordAlignments,
foreign => foreign.Id,
(AggregatedAlignedComponentRecordDataModel lookupResult) =>lookupResult
);
// var expectedLookup =
// @"{$lookup: { from: 'componentRecord', localField: 'recordAlignments', foreignField: '_id', as: 'alignedToRecord' } }";
var rendered = lookup?.Render(BsonSerializer.SerializerRegistry.GetSerializer<ComponentRecordDataModel>(),
BsonSerializer.SerializerRegistry);
}