[SERVER-9023] $add returns null if any of the operands are null Created: 19/Mar/13  Updated: 10/Dec/14  Resolved: 21/Mar/13

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

Type: Bug Priority: Major - P3
Reporter: Travis Dahlke Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB 2.4.0, Mac OS X 10.8.2


Operating System: ALL
Steps To Reproduce:

> db.things.insert({a:2,b:3})
> db.things.aggregate({$project:{total:{$add:["$a","$b"]}}})
{
	"result" : [
		{
			"_id" : ObjectId("5148c8d7d2f22a5f45c45d64"),
			"total" : 5
		}
	],
	"ok" : 1
}
> db.things.aggregate({$project:{total:{$add:["$a","$b","$c"]}}})
{
	"result" : [
		{
			"_id" : ObjectId("5148c8d7d2f22a5f45c45d64"),
			"total" : null
		}
	],
	"ok" : 1
}

Participants:

 Description   

In version 2.2, the aggregation framework $add operator correctly added all non-null operands. In version 2.4, if any one of the operands is null, it returns null.



 Comments   
Comment by Ian Whalen (Inactive) [ 21/Mar/13 ]

Hi Travis, this was an intentional design decision made for 2.4.0 that you can read about in SERVER-7932. As in that ticket, you can use $ifNull if you prefer the old behavior.

Generated at Thu Feb 08 03:19:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.