I have a like array that contains a list of users who like images embedded in the document. I want to return boolean value when user likes photo with the user_id parameter passed.
I use $eq aggregate but it only accepts the latest object of like array. If an array has only one object it will return the correct value, If the array has more than one object it will only look in the latest object. I have to do so that it can find all objects in like array and return the correct value.
"likes" : [
{
"user_id" : ObjectId("")
},
{
"user_id" : ObjectId("")
}
],
db.getCollection('photos').aggregate(
[
{
$match: {
status: '1'
}
},
{
$project: {
like_count:{$size:"$likes"},
is_liked: {$eq: ["$likes.user_id", [ObjectId("")]]},
_id:1,
title:1
}
},
]
);
the useris a single user?likesarray.