I can create a projection fiel definition from expression like that:
var projection = Builders<Bar>.Projection.Expression(x => x.Foo)
I want to do exactly the same with the filter:
var filter = Builders<Bar>.Filter.Expression(x => x.Foo == "10")
But I cannot find a way to do this. The operations that I want to perform FindOneAndUpdateAsync
supports an expression as it's input directly, but I want to combine a "hardcoded" filter with the one that is coming in as a parameter. This is how I do it now without an expression:
var filter = Filter.Eq(x => x.Id, id); // Here I would like to write Filter.Expression(x => x.Id == id)
if (additionalFilter != null)
{
filter &= additionalFilter;
}
P.S. I would rather avoid combining expressions directly "the C# way" as it is not very easy to understand.
CodePudding user response:
Mongo net driver does not support Filter.Expression
and if you want to project any value cast to as ToList
See the below eg:
var filter = Builders<Movie>.Filter.In("cast", new string[] { "Tom Hanks" });
var movies = await _moviesCollection.Find<Movie>(filter)
.Limit(2)
.ToListAsync();
// your test
var projection = Builders<Bar>.Projection.Expression(x => x.Foo);
var projection = Builders<Bar>.Filter.Where(x => x.Foo == "10");