Seeking Insights on LINQ to MongoDB Query Translation Issue

Greetings MongoDB Community,

I am currently engaged in a project that involves translating LINQ Expressions to MongoDB queries. During this process, I encountered a peculiar issue that I suspect might be related to either the MongoDB LINQ provider’s translation capabilities or an unsupported query feature. I’m reaching out to the community for insights or confirmation on whether this is a known limitation or a potential bug.

I’m utilizing .NET 6.0 and MongoDB.Driver version 2.22.0 from NuGet.

Here’s the LINQ expression in question:

.Take(25)
.Where(x => 
    (
        (x.Cpf == "...") && 
        (x.FirstName == "Rodrigo") &&
        (
            x.Surnames.Any(x => 
                (x == "Jacques") || 
                (x == "Amanda")
            ) &&
            (x.Surnames.Count() > 1)
        )
    )
)

This LINQ expression is then translated into the following MongoDB aggregation query:

[
    { "$limit": NumberLong(25) },
    { 
        "$match": {
            "$and": [
                { "Cpf": "11548128988" },
                { "FirstName": "Rodrigo" },
                { 
                    "Surnames": {
                        "$elemMatch": {
                            "$or": [
                                { "$eq": "Jacques" },
                                { "$eq": "Amanda" }
                            ]
                        }
                    }
                },
                { 
                    "$expr": {
                        "$gt": [
                            { "$size": "$Surnames" },
                            1
                        ]
                    }
                }
            ]
        } 
    }
]

Upon executing this query, I encounter the following exception:

MongoCommandException: Command aggregate failed: unknown top level operator: $eq. If you have a field name that starts with a '$' symbol, consider using $getField or $setField.

The LINQ expression was generated during the synthesis phase of a custom language developed for web purposes. It compiles seamlessly into C# code and functions correctly with other IEnumerable types, which suggests that the LINQ aspect should be reliable. All nuances, such as ExpressionParameter references, have been thoroughly considered during the final Expression formulation.

Any guidance or insights into whether this is a known issue with the MongoDB LINQ provider, or perhaps a limitation in the supported query features, would be greatly appreciated.

Thank you in advance for your time and assistance.

Best regards, Jacques.

Hi, @Rodrigo_Jacques1,

Welcome to the MongoDB Community Forums and thank you for reaching out.

The isse that you’ve encountered is CSHARP-2348. You can resolve this issue by splitting Any(x => predicate1 || predicate2) to Any(predicate1) || Any(predicate2). For example:

var query = coll.AsQueryable()
    .Take(25)
    .Where(x =>
        (
            (x.Cpf == "...") &&
            (x.FirstName == "Rodrigo") &&
            (
                (x.Surnames.Any(y => y == "Jacques") ||
                 x.Surnames.Any(y => y == "Amanda")) &&
                (x.Surnames.Count() > 1)
            )
        )
    );

Please upvote and follow CSHARP-2348 for additional updates.

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.