-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Blocker - P1
-
None
-
Affects Version/s: 2.15.0
-
Component/s: BSON
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 },
{{
)
)
},
{ "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.