-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Environment:OS: macOS
node.js / npm versions:
Additional info:
-
Not Needed
-
Developer Tools
Problem Statement/Rationale
Since there is no "$exists" clause in aggregations, we're checking for undefined. However, $eq & $ne checks don't work with either undefined or null. When we change the operators to anything else, ex. $gt, $gte, it works.
The following does not work
{
$group: {
_id: 1,
HasTranscriptId: {
$sum: {
$cond: [
{ $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
]
}
}
}
}
When you change the operator to something other than $eq / $ne, the query works.
{
_id: { $gt: ["$Data.Metadata.TranscriptId", undefined] },
HasTranscriptId: {$count: { } }
}
Please be sure to attach relevant logs with any sensitive data redacted.
How to retrieve logs for: Compass; Shell
Steps to Reproduce
Try checking for the existence of a field in an aggregation stage, ex. $group or $project. You can try to compare with null or undefined, the results are the same.
Expected Results
Expecting this accumulator to work with $eq / $ne:
{
$group: {
_id: 1,
HasTranscriptId: {
$sum: {
$cond: [
{ $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
]
}
}
}
}
Actual Results
$eq / $ne does not work.
{
$group: {
_id: 1,
HasTranscriptId: {
$sum: {
$cond: [
{ $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
]
}
}
}
}
Additional Notes
Alternatively, it would be great if $exists is expanded to work in aggregation stages or a similar operator is added.
Results:
query:
{
_id: { $gt: ["$Data.Metadata.TranscriptId", undefined] },
HasTranscriptId: {$count: { } }
}
output:
[{_id: false, HasTranscriptId: 6996}, {_id: true, HasTranscriptId: 3525}]
query:
{
_id: { $eq: ["$Data.Metadata.TranscriptId", undefined] },
HasTranscriptId: {$count: { } }
}
output:
[{_id: false, HasTranscriptId: 10521}]