[CSHARP-4861] Count comparison with constant on the left is not translated correctly Created: 30/Nov/23  Updated: 07/Dec/23  Resolved: 07/Dec/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.22.0
Fix Version/s: 2.23.0

Type: Bug Priority: Critical - P2
Reporter: Mikhail Khalizev Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Linq query to check collection items count is not correctly translate to mongo aggregate pipeline.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

MongoDB.Driver 2.22.0

How to Reproduce

public class Document
{
    public HashSet<string> Tokens { get; set; } = new();
}
 
public void CheckTokensCountQuery()
{
    var collection = Database.GetCollection<Document>("test");
    var query = collection
        .AsQueryable()
        .Where(x => 1 < x.Tokens.Count);
 
    var queryString = query.ToString();
 
    // queryString is:
    // test.test.Aggregate([{ "$match" : { "Tokens.0" : { "$exists" : false } } }])
}

As you can see above, `queryString` doesn't actually check that the size of the `Tokens` collection is greater than 1. Instead, it checks that the `Tokens` collection is empty.



 Comments   
Comment by Githook User [ 07/Dec/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4861: Count comparison with constant on the left is not translated correctly.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/86620014b862066d33d8d2bbfef2bbec0588421e

Comment by Robert Stam [ 30/Nov/23 ]

Thank you for reporting this issue. I am able to reproduce it and I have a fix in code review.

Comment by PM Bot [ 30/Nov/23 ]

Hi exomicky53@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.