[SERVER-26180] is this a bug in $cond in $project, or am i using it wrong? Created: 20/Sep/16  Updated: 27/Sep/16  Resolved: 20/Sep/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.2.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: tony kerz Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-13903 inconsistent treatment of null in $pr... Backlog
Operating System: ALL
Steps To Reproduce:

see above...

Participants:

 Description   

here's some data:

> db.scratch.find()
{ "_id" : ObjectId("57e0bac25e54b80da88af731"), "a" : "baz" }
{ "_id" : ObjectId("57e0bac75e54b80da88af732"), "a" : "foo", "b" : "bar" }

here's a pipeline, desired effect is to concat a and b if i have both fields, or just use a if b isn't present:

> db.scratch.aggregate([{$project: {ab: {$cond: [{$eq: ['$b', null]}, '$a', {$concat: ['$a', ':', '$b']}]}}}])
{ "_id" : ObjectId("57e0bac25e54b80da88af731"), "ab" : null }
{ "_id" : ObjectId("57e0bac75e54b80da88af732"), "ab" : "foo:bar" }

but when b is not present and i want a i get null... ?!



 Comments   
Comment by Asya Kamsky [ 27/Sep/16 ]

Meanwhile a workaround would be to add explicit "ifNull" to convert missing "$b" into explicit "null":

db.scratch.aggregate([{$project: {ab: {$cond: [{$eq: [{$ifNull:['$b',null]}, null]}, '$a', {$concat: ['$a', ':', '$b']}]}}}])
{ "_id" : ObjectId("57e0bac25e54b80da88af731"), "ab" : "baz" }
{ "_id" : ObjectId("57e0bac75e54b80da88af732"), "ab" : "foo:bar" }

Comment by Kelsey Schubert [ 20/Sep/16 ]

Hi tony-kerz,

Thank you for reporting this behavior. We are tracking this bug in SERVER-13903. Please feel free to vote for it and watch it for updates.

Kind regards,
Thomas

Generated at Thu Feb 08 04:11:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.