I was using the Equals(obj)
method, but it's not working correctly because the array elements in the database are not ordered. If the order is different, it is not considered a matching element.
I tried that solution in two ways:
var filteredObj = objects.Find(obj => obj.Elements.OrderBy(x => x).Equals(elements)).FirstOrDefault();
var filteredObj = objects.Find(obj => obj.Elements.OrderBy(x => x).Equals(elements)).FirstOrDefault();
But in both of them I get the following exception:
Unsupported filter: {document}{Elements}.Equals(value(System.Collections.Generic.List[System.Int32]).OrderBy(x => x)).
It seems that MongoDb Driver doesn't support that kind of thing.
The method responsible for that:
public string GetIdByElements(List<int> elements)
{
var filteredObj = objects.Find(obj => obj.Elements.Equals(elements)).FirstOrDefault();
if (filteredObj is null)
throw new ObjectNotFoundException("Object not found");
return filteredObj.Id.ToString();
}