[CSHARP-4148] $sum using $cond in a $group returns different results when used in the C# driver versus when run in Compass Created: 23/Apr/22  Updated: 27/Oct/23  Resolved: 25/Apr/22

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.15.0
Fix Version/s: None

Type: Bug Priority: Blocker - P1
Reporter: Steen Tøttrup Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: aggregate, conditional_operator
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

$sum, in a $group using $cond fails when using working code (from Compass) in c# code. C# driver version is 2.15.0, server is hosted on: cluster0.lg9qe.mongodb.net (shared) and the server version is 5.0.7 afaik

How to Reproduce

I've been building an aggregation using Compass, with a few stages, and got the result I wanted/expected. When done I got Compass to generate the pipeline code needed in C# to get the same result.

When running the generated code using the C# driver, the result is different.

The stage code for the $group is this:

  _id: {$dateToString: { format: "%Y-%m-%d", date: {$add: [ISODate('1970-01-01T00:00:00Z'),  {$multiply: [1000,"$StartGameTime"]  }]},}}
 won: {
        $sum: {
          "$cond": {
            "if": {
              $eq: [
                "$team",
                "$winner"
              ]
            },
            "then": 1,
            "else": 0

{\{          }}} {\{        }}}
      },
      lost: {
        $sum: {
          "$cond": {
            "if": {
              $ne: [
                "$team",
                "$winner"
              ]
            },
            "then": 1,
            "else": 0{{          }

}}

{\{        }

}}
      }}{}{}

 

And this in C#:

               .AppendStage<Dtos.DailyWonLostStats>(
                 new BsonDocument("$group",
                    new BsonDocument {
                        { "_id", 
                            new BsonDocument("$dateToString", 
                            new BsonDocument {
                                { "format", "%Y-%m-%d" },
                                { "date",
                                    new BsonDocument("$add",
                                    new BsonArray {
                                        new DateTime(1970, 1, 1, 0, 0, 0),
                                        new BsonDocument("$multiply",
                                        new BsonArray {
                                            1000,
                                            "$StartGameTime"
                                        })
                                    })

{\{                                }

}}
                            })
                        },
                        { "won",
                            new BsonDocument("$sum",
                            new BsonDocument("$cond",
                            new BsonDocument {
                                { "if",
                                    new BsonDocument("$eq",
                                    new BsonArray {
                                        "$team",
                                        "$winner"
                                    })
                                },
                                { "then", 1 },
{{                               

{ "else", 0 }}} {\{                            }}}
                            )
                            )
                        },
                        { "lost",
                            new BsonDocument("$sum",
                            new BsonDocument("$cond",
                            new BsonDocument {
                                { "if",
                                    new BsonDocument("$ne",
                                    new BsonArray
                                    {
                                        "$team",
                                        "$winner"
                                    })
                                },
                                { "then", 1 },
{{                                { "else", 0 }

}}

{\{                            }

}}
                            )
                            )

{\{                        }

}}
                    })
                )

The result in Compass looks like this (just one example):

_id:"2022-02-28"
won:6
lost:4

And same code from C# (data from an endpoint, properties have other names, using BsonElement to map it):

{{

{ dateString: "2022-02-28", wins: 10, loses: 0 }

}}

And this is the pattern I see with for all days, instead of having values in both properties, wins is the sum of the actual won and lost fields, and loses will be zero. Always.

I'm sorry for not providing a working example, but I'm not sure where I could host some simplified data with access for the public.

Additional Background

Not sure what else to tell.



 Comments   
Comment by Robert Stam [ 25/Apr/22 ]

No worries. Thanks for letting us know!

Comment by Steen Tøttrup [ 23/Apr/22 ]

STOP

As so often before, the second after writing the issue, it dawned on me what was the problem. PLEASE just close this one again!

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