[CSHARP-1974] Count Expression in Aggregation Definition silently does the wrong thing Created: 02/May/17  Updated: 28/Oct/23  Resolved: 15/Jan/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4.3
Fix Version/s: 2.8.0

Type: Bug Priority: Major - P3
Reporter: Kirill Rakhman Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

Consider the following aggregation defintion

DateTime now = DateTime.Now;
Collection.Aggregate().Group(x => x.AdapterShortKey, group => new { CountFoo = group.Count(x => x.DepartureDate < now) });

This code compiles and runs but does the wrong thing. Instead of counting the documents with the condition

x.DepartureDate < now

it counts all the documents.

The workaround is to write

group.Sum(x => x.DepartureDate < now ? 1 : 0)

which does the correct thing.



 Comments   
Comment by Githook User [ 15/Jan/19 ]

Author:

{'username': 'dmitry-lukyanov', 'email': 'dmitry_lukyanov@epam.com', 'name': 'Dmitry Lukyanov'}

Message: CSHARP-1974: Fix Count with a predicate in aggregate group.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/8fb445051c02918d9be32e9a4008d2bf7c454532

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