$setUnion returns the all element set appears in input sets and
accepts any number of expressions.
- $setUnion accepts any number of expressions.
- $setUnion ignores duplicate elements in sets.
- $setUnion ignores order o elements.
- $setUnion treat as elements to the nested array.
Syntax:
{ $setIntersection: [ <array1>, <array2>, ... ] }
|
Example:
Create
a collection as following.
{_id:1,like:["Dilip","Ashish"],comment:["Dilip","Ashish"]}
{_id:2,like:["Dilip","Ashish"],comment:["Dilip","Dilip","Ashish"]}
{_id:3,like:["Dilip","Ashish"],comment:["Dilip","Mamta","Ashish"]}
{_id:4,like:["Mahesh","Vipul"],comment:["Dilip","Ashish"]}
{_id:5,like:["Mahesh","Vipul"],comment:[["Mahesh","Vipul"]]}
{_id:6,like:[],comment:[]}
{_id:7,like:[],comment:[[]]}
|
Run
following query
db.LikeAndComment.aggregate(
[
{ $project: { like: 1,
comment: 1, eleUnion: { $setUnion: [ "$like", "$comment"
] }, _id: 0 } }
]
)
|
Result:
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Ashish" ],
"eleUnion" : [ "Dilip", "Ashish" ] }
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Dilip",
"Ashish" ], "eleUnion" : [ "Dilip",
"Ashish" ] }
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Mamta",
"Ashish" ], "eleUnion" : [ "Dilip",
"Ashish", "Mamta" ] }
{ "like" : [ "Mahesh", "Vipul" ],
"comment" : [ "Dilip", "Ashish" ],
"eleUnion" : [ "Mahesh", "Ashish",
"Vipul", "Dilip" ] }
{ "like" : [ "Mahesh", "Vipul" ],
"comment" : [ [ "Mahesh", "Vipul" ] ],
"eleUnion" : [ "Mahesh", "Vipul", [
"Mahesh", "Vipul" ] ] }
{ "like" : [], "comment" : [],
"eleUnion" : [] }
{ "like" : [], "comment" : [ [] ],
"eleUnion" : [ [] ] }
|
No comments:
Post a Comment
Please do not enter any spam link in the comment box.