Its returns common elements appear in all input sets and
accept any number of documents.
- $setIntersection takes only two input
expression
- $setIntersection treats like sets to arrays
- $setIntersection ignores the duplicates
- $setIntersection ignores the order of elements
- $setIntersection filters duplicates elements
- $setIntersection treats as element to 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, eleIntersect:
{ $setIntersection: [ "$like", "$comment" ] }, _id: 0 } }
]
)
|
Result:
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Ashish" ],
"eleIntersect" : [ "Dilip", "Ashish" ] }
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Dilip", "Ashish"
], "eleIntersect" : [ "Dilip", "Ashish" ] }
{ "like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Mamta",
"Ashish" ], "eleIntersect" : [ "Dilip",
"Ashish" ] }
{ "like" : [ "Mahesh", "Vipul" ],
"comment" : [ "Dilip", "Ashish" ],
"eleIntersect" : [] }
{ "like" : [ "Mahesh", "Vipul" ],
"comment" : [ [ "Mahesh", "Vipul" ] ],
"eleIntersect" : [] }
{ "like" : [], "comment" : [],
"eleIntersect" : [] } /* 7 */ { "like" : [],
"comment" : [ [] ], "eleIntersect" : [] }
|
No comments:
Post a Comment
Please do not enter any spam link in the comment box.