Let's say I have a collection like :
{
id: "1"
name: "collection 1"
properties: "Some properties."
}
With a class representation as
[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; }
}
Now if am going to create a Projection like
Builders<InfoPOCO>.Projection.Include(_ => new{_.Name});
And call it with other params ( That is working fine without projection )
return GetDataBase().GetCollection<InfoPOCO>(collectionName).Find(Expr).
Project<InfoPOCO>(projectionDefinition).Skip(Offset).Limit(Limit).Sort(sort).ToList<InfoPOCO>()
Then I get the following error:
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)
What could be the reason for it, when am not using projection, it is fetching the entire collection fine. I don't know if I need to map the classes? (Thought Auto Map would take it) . I tried making the constructor in the class too. But still the same error. Any help would be highly appreciated. Thank you !
I finally understood! Yeah, I know, kinda slow, duh.
The issue is that when I was trying to map it like :
Builders<InfoPOCO>.Projection.Include(_ => new{_.Name, _.Properties});
It's a strongly typed expression and the result is mapped to an anonymous type. If am using forEach at the same time as am fetching it, then I can get those values. So the solution I am using right now is simple. I changed my projection to:
Builders<InfoPOCO>.Projection.Include(_ => _.Name).
Include(_ => _.Properties);
This works! Am not saying its the perfect solution or explanation but I see a lot of people struggling with it, so just mentioning a workaround. Hope it helps someone like me from going through hours of googling!