I am trying to fetch a Document from MongoDB using the C# MongoDB driver.
public class Record
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Data { get; set; }
}
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
var Names = new List<Names>;
Names.Add(new Name(FirstName = "abc", LastName = "xyz"));
Names.Add(new Name(FirstName = "123", LastName = "789"));
Names.Add(new Name(FirstName = "a1b2", LastName = "c7d8"));
I tried the below query to filter and fetch the required document. But this query is failing and throwing an exception.
FilterDefinition<Record> patentFilter = Builders<Record>.Filter.Where(y => Names.Any(x=> x.Name == y.Name && x.LastName == y.LastName));
Exception:
System.ArgumentException: 'Unsupported filter: Any(value(Name]).Where((({document}{FirstName} == {document}{FirstName}) AndAlso ({document}{LastName} == {document}{LastName})))).'
Late for the answer.
Would suggest:
Generate
nameFilters.Each
nameFiltermust meet equal ($eq) for bothFirstNameand ($and)LastName.Parent filter (
filter) chains thenameFilterswith$or.Any document that fulfills the
nameFilterwill be returned.Sample Mongo Playground