It’s accept only two
argument expression and returns the set of element who appears in first not in
second
- $setDifference takes only two input expression
- $setDifference treats like sets to arrays
- $setDifference ignores the duplicates
- $setDifference ignores the order of elements
- $setDifference filters duplicates elements
- $setDifference treats as element to nested array
Syntax:
{ $setDifference: [
<expression1>, <expression2>] }
|
Example:
Create a Collection as following
{_id:1,like:["Dilip","Ashish","Mamta"],comment:["Dilip","Ashish"]}
{_id:2,like:["Dilip","Ashish","Raj"],comment:["Dilip","Dilip","Ashish"]}
{_id:3,like:["Dilip","Ashish"],comment:["Dilip","Mamta","Ashish"]}
{_id:4,like:["Mahesh","Vipul"],comment:["Dilip","Ashish"]}
{_id:7,like:["Akash"],comment:[["Ravi"]]}
|
Run
Following setDifference query
db.LikeAndComment.aggregate(
[
{ $project: { like: 1, comment: 1,
eleDiff: { $setDifference: [ "$like", "$comment" ] },
_id: 0 } }
]
)
|
Result:
{ "like" : [ "Dilip", "Ashish",
"Mamta" ], "comment" : [ "Dilip", "Ashish" ], "eleDiff" :
[ "Mamta" ] }
{ "like" : [ "Dilip", "Ashish",
"Raj" ], "comment" : [ "Dilip", "Dilip",
"Ashish" ], "eleDiff" : [ "Raj" ] }
{ "like" : [ "Dilip", "Ashish" ], "comment" :
[ "Dilip", "Mamta", "Ashish" ], "eleDiff" :
[] }
{ "like" : [ "Mahesh", "Vipul" ], "comment" :
[ "Dilip", "Ashish" ],
"eleDiff" : [ "Mahesh",
"Vipul" ] }
{
"like" : [ "Akash" ], "comment" :
[ [ "Ravi" ] ], "eleDiff"
: [ "Akash" ] }
|
No comments:
Post a Comment
Please do not enter any spam link in the comment box.