C# MongoDB & 投影。序列化异常失败
C# MongoDB & Projection. Fails with serialization Exception
假设我有一个像这样的集合:
{
id: "1"
name: "collection 1"
properties: "Some properties."
}
class表示为
[BsonIgnoreExtraElements]
public class InfoPOCO {
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("properties")]
public string Properties { get; set; }
}
现在,如果我要创建一个类似
的投影
Builders<InfoPOCO>.Projection.Include(_ => new{_.Name});
并用其他参数调用它(没有投影也能正常工作)
return GetDataBase().GetCollection<InfoPOCO>(collectionName).Find(Expr).
Project<InfoPOCO>(projectionDefinition).Skip(Offset).Limit(Limit).Sort(sort).ToList<InfoPOCO>()
然后我得到以下错误:
System.InvalidOperationException : Unable to determine the serialization information for
_ => new <>f__AnonymousType2`1
Result StackTrace:
at MongoDB.Driver.ExpressionFieldDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.SingleFieldProjectionDefinition`1.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.KnownResultTypeProjectionDefinitionAdapter`2.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass35_0`1.<FindSync>b__0(IClientSessionHandle session)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
可能是什么原因,当我不使用投影时,它正在获取整个集合。我不知道是否需要映射 classes? (以为 Auto Map 会接受它)。我也尝试在 class 中制作构造函数。但还是一样的错误。任何帮助将不胜感激。谢谢 !
我终于明白了!是的,我知道,有点慢,呃。
问题是当我试图像这样映射它时:
Builders<InfoPOCO>.Projection.Include(_ => new{_.Name, _.Properties});
这是一个强类型表达式,结果映射到匿名类型。如果在获取它的同时使用 forEach,那么我可以获得这些值。所以我现在使用的解决方案很简单。我将投影更改为:
Builders<InfoPOCO>.Projection.Include(_ => _.Name).
Include(_ => _.Properties);
这行得通!我并不是说它是完美的解决方案或解释,但我看到很多人都在为它苦苦挣扎,所以只提一个解决方法。希望它能帮助像我这样的人度过数小时的谷歌搜索!
假设我有一个像这样的集合:
{
id: "1"
name: "collection 1"
properties: "Some properties."
}
class表示为
[BsonIgnoreExtraElements]
public class InfoPOCO {
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("properties")]
public string Properties { get; set; }
}
现在,如果我要创建一个类似
的投影Builders<InfoPOCO>.Projection.Include(_ => new{_.Name});
并用其他参数调用它(没有投影也能正常工作)
return GetDataBase().GetCollection<InfoPOCO>(collectionName).Find(Expr).
Project<InfoPOCO>(projectionDefinition).Skip(Offset).Limit(Limit).Sort(sort).ToList<InfoPOCO>()
然后我得到以下错误:
System.InvalidOperationException : Unable to determine the serialization information for
_ => new <>f__AnonymousType2`1
Result StackTrace:
at MongoDB.Driver.ExpressionFieldDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.SingleFieldProjectionDefinition`1.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.KnownResultTypeProjectionDefinitionAdapter`2.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass35_0`1.<FindSync>b__0(IClientSessionHandle session)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
可能是什么原因,当我不使用投影时,它正在获取整个集合。我不知道是否需要映射 classes? (以为 Auto Map 会接受它)。我也尝试在 class 中制作构造函数。但还是一样的错误。任何帮助将不胜感激。谢谢 !
我终于明白了!是的,我知道,有点慢,呃。
问题是当我试图像这样映射它时:
Builders<InfoPOCO>.Projection.Include(_ => new{_.Name, _.Properties});
这是一个强类型表达式,结果映射到匿名类型。如果在获取它的同时使用 forEach,那么我可以获得这些值。所以我现在使用的解决方案很简单。我将投影更改为:
Builders<InfoPOCO>.Projection.Include(_ => _.Name).
Include(_ => _.Properties);
这行得通!我并不是说它是完美的解决方案或解释,但我看到很多人都在为它苦苦挣扎,所以只提一个解决方法。希望它能帮助像我这样的人度过数小时的谷歌搜索!