I get NullReferenceException when running next simple query:
int? currentUserId = null;
_client.GetDatabase("mydatabase").GetCollection<MyDocument>("mycollection").Aggregate().Match(m => m.CustomerId == currentUserId && m.Participants.Any(x => x.CustomerId == currentUserId)).FirstOrDefault();
When I initialize "currentUserId " with value, it runs fine.
In my example MyDocument.CustomerId is Nullable<int> and property on inned collection Participant.CustomerId is int.
Looks like it depends on type of property of document.
If property is non-nullable (i.e. int) but variable in expression is nullable (currentUserId in this example) - than it tries to get value from variable no matter what.
Also, looks like queries are not getting evaluated and simplified before being serialized. Consider this example:
int? currentUserId = null;
_client.GetDatabase("mydatabase").GetCollection<MyDocument>("mycollection").Aggregate()Match(m => currentUserId != null && m.CustomerId == currentUserId && m.Participants.Any(x => x.CustomerId == currentUserId)).FirstOrDefault();
If currentUserId is null (which is known at the time of traversing expression tree), than whole condition will be evaluated to false no matter what go after (because of and && operation used). So in such case query can be first simplified to simple (m => false) and then serialized.