Operational Defect Database

BugZero found this defect 2440 days ago.

MongoDB | 396914

[SERVER-29789] $setUnion returns values from second array instead of union

Last update date:


Affected products:

MongoDB Server

Affected releases:


Fixed releases:

No fixed releases provided.



When execute { $setUnion: { [ "a", "b", "c" ], [ "b", "a", "a", "d" ] }} or { $setUnion: [ "a", "b", "c" ], [ "b", "a", "a", "d" ] } either returns: "b", "a", "a", "d" 1. union does not work. expected result for union should include: "c", "b", "a", "d" 2. duplicated items do not removed from the result.

Top User Comments

mark.agarunov commented on Thu, 6 Jul 2017 20:42:29 +0000: Hello apetruc@gmail.com, Thank you for the response. As $setUnion is an aggregation operation, it can only be used inside the aggregation pipeline. When attempting it use it directly in the shell, it is not actually being parsed as an operation, but as an empty/null variable with the name $setUnion. Essentially what the shell is evaluating becomes: {null: [ "a", "b", "c" ], null:[ "b", "a", "a", "d" ] } Since object properties cannot have the same name, it evaluates to the last value and returns just [ "b", "a", "a", "d" ] The second example evaluates to: {null: [[ "a", "b", "c" ], [ "b", "a", "a", "d" ]] } Which then simplifies to: [[ "a", "b", "c" ], [ "b", "a", "a", "d" ]] As this is properly working when used in the aggregation pipeline, I do not see anything to indicate a bug in the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group. Thanks, Mark apetruc@gmail.com commented on Thu, 6 Jul 2017 11:23:06 +0000: Hi, your code works as you described. However > { $setUnion: [ "a", "b", "c" ], [ "b", "a", "a", "d" ] } //Run only this code output: "b", "a", "a", "d" { $setUnion: [[ "a", "b", "c" ], [ "b", "a", "a", "d" ]] } //Run only this code output [ "a", "b", "c"], [ "b", "a", "a", "d"] in first variant it returns only second array. In second it returns 2 arrays instead of one flatten array with all values. thomas.schubert commented on Thu, 22 Jun 2017 18:40:21 +0000: Hi apetruc@gmail.com, Could you clarify the steps you're taking to reproduce this issue? If possible, could you upload a script that shows this behavior? I was unable to observe the issue describe by the taking the steps below: > db.version() 3.2.3 > db.foo.insert({}) WriteResult({ "nInserted" : 1 }) > db.foo.aggregate( [ { $project: {allValues: { $setUnion: [ [ "a", "b", "c" ], [ "b", "a", "a", "d"] ] }, _id: 0 } } ] ) { "allValues" : [ "d", "c", "b", "a" ] } Thanks for your help, Thomas apetruc@gmail.com commented on Thu, 22 Jun 2017 11:11:16 +0000: same for version 3.2.9

Additional Resources / Links


BugZero Risk Score

Coming soon



Have you been affected by this bug?


Do you know how much operational outages are costing you?

Understand the cost to your business and how BugZero can help you reduce those costs.


Login to read and write comments.

Have you ever...

had your data corrupted from a