[SERVER-29789] $setUnion returns values from second array instead of union Created: 22/Jun/17  Updated: 27/Oct/23  Resolved: 06/Jul/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: anatolii petruchok Assignee: Mark Agarunov
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

execute script
{ $setUnion: { [ "a", "b", "c" ], [ "b", "a", "a", "d" ] }}
or

{ $setUnion: [ "a", "b", "c" ], [ "b", "a", "a", "d" ] }
Participants:

 Description   

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.



 Comments   
Comment by Mark Agarunov [ 06/Jul/17 ]

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

Comment by anatolii petruchok [ 06/Jul/17 ]

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.

Comment by Kelsey Schubert [ 22/Jun/17 ]

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

Comment by anatolii petruchok [ 22/Jun/17 ]

same for version 3.2.9

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