-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Critical - P2
-
None
-
Affects Version/s: 2.4.4, 2.5
-
Component/s: Read Operations
-
None
-
Environment:windows 7sp1, visual studio 2017, driver 2.4.4, 2.5.0, server 3.4.10
A class inside an embedded array is not being serialized when Projection.ElemMatch is used
I have classes like this:
internal class AccountDal { public Guid Id { get; set; } // more properties public List<AccountMemberDal> Members { get; set; } } internal class AccountMemberDal { public RegisteredEntityDal Member { get; set; } public EAccountPermissions Permissions { get; set; } // some enum public bool IsAdmin { get; set; } public bool IsDefaultAccount { get; set; } } public class RegisteredEntityDal { public Guid Id { get; set; } public EEntityTypeDal Type { get; set; } // some enum }
I constructed a query:
FilterDefinition<AccountDal> filter = Builders<AccountDal>.Filter.And( Builders<AccountDal>.Filter.Eq(a => a.IsBusiness, true), Builders<AccountDal>.Filter.ElemMatch(a => a.Members, Builders<AccountMemberDal>.Filter .And( Builders<AccountMemberDal>.Filter.Eq(a => a.IsAdmin, true), Builders<AccountMemberDal>.Filter.Eq(a => a.IsDefaultAccount, false) )) ); ProjectionDefinition<AccountDal, AccountMemberDal> project = Builders<AccountDal> .Projection.ElemMatch( a => a.Members, Builders<AccountMemberDal>.Filter.And( Builders<AccountMemberDal>.Filter.Eq(a => a.IsAdmin, true), Builders<AccountMemberDal>.Filter.Eq(a => a.IsDefaultAccount, false) )); // or // ProjectionDefinition<AccountDal, AccountMemberDal> project = Builders<AccountDal> // .Projection.Include(a => a.Members[-1]); // which one is recommendes? //and execute the query: IMongoCollection<AccountDal> accounts = DbGeneralContext.Accounts; var result = await accounts.Find(filter).Project(project).ToListAsync(); // the query that is being generated is: db.accounts.find({ "isBusiness" : true, "members" : { "$elemMatch" : { "isAdmin" : true, "isDefaultAccount" : false } } },{ "members" : { "$elemMatch" : { "isAdmin" : true, "isDefaultAccount" : false } } })
with the following result:
{ "_id" : BinData(4,"KKExCpz25hGbRAAkmxAf2Q=="), "members" : [ { "member" : { "id" : BinData(4,"Y+gHIJv25hGbRAAkmxAf2Q=="), "type" : 1 }, "permissions" : 1, "isAdmin" : true, "isDefaultAccount" : false } ] } { "_id" : BinData(4,"L/pKS93w5hGfMgAkmxAf2Q=="), "members" : [ { "member" : { "id" : BinData(4,"JAUR4Hbc5hGfyQAkmxAf2Q=="), "type" : 1 }, "permissions" : 1, "isAdmin" : true, "isDefaultAccount" : false } ] }
However the c# object does not serialize the 'Member' property and it remains null
see attached 'account-member-result.png' result
Thanks,
Itzhak