This operator comes in MongoDB version 2.6, $setEquals
compares the arrays and returns true or false. $setEquals returns true if
arrays have the same distinct element if they don’t then return false.
- $setEquals treat to arrays like sets.
- $setEquals ignore duplicate entry in the array.
- $setEquals ignore the order of elements.
- $setEquals evaluate at the top level to the nested array.
Syntax:
{
$setEquals: [ <expression1>, <expression2>, ... ] }
|
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 the following query
db.LikeAndComment.aggregate( [
{ $project: { like: 1, comment: 1,
setEle: { $setEquals: [ "$like", "$comment" ] }, _id: 0 }
}
]
)
|
Result:
{
"like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Ashish" ],
"setEle" : true }
{
"like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Dilip",
"Ashish" ], "setEle" : true }
{
"like" : [ "Dilip", "Ashish" ],
"comment" : [ "Dilip", "Mamta",
"Ashish" ], "setEle" : false }
{
"like" : [ "Mahesh", "Vipul" ],
"comment" : [ "Dilip", "Ashish" ],
"setEle" : false }
{
"like" : [ "Mahesh", "Vipul" ],
"comment" : [ [ "Mahesh", "Vipul" ] ],
"setEle" : false }
{
"like" : [], "comment" : [], "setEle" : true }
{
"like" : [], "comment" : [ [] ], "setEle" :
false }
|
No comments:
Post a Comment
Please do not enter any spam link in the comment box.