I have photos_comments collection:
{
"_id" : ObjectId(""),
"photo_id" : ObjectId(""),
"content" : "Comment 1",
"is_approved" : "1",
"user" : {
"user_id" : ObjectId(""),
"name" : "",
"avatar" : ".jpg"
},
"likes" : [
{
"user_id" : ObjectId("")
}
],
"like_count" : 1,
"reply_count" : 5,
"replies" : [
{
"reply_id" : ObjectId(""),
"content" : "Reply 1",
"is_approved" : "1",
"user" : {
"user_id" : ObjectId(""),
"name" : "",
"avatar" : ".jpg"
},
"likes" : [
{
"user_id" : ObjectId("")
}
],
"like_count" : 1
},
]
}
I use $in to check if current user like reply comment:
$collection->aggregate(
[
[
'$match' => [
'_id' => new ObjectId($comment_id)
]
],
[
'$project' => [
'replies.reply_id' => 1,
'replies.like_count' => 1,
'replies.content' => 1,
'replies.is_liked' => ['$in' => [['user_id' => $user_id], '$replies.likes']],
'replies.user' => 1,
'replies.created_at' => 1
]
],
['$sort' => ["replies.reply_id" => 1]],
['$limit' => 5]
]);
But replies.is_liked alway return false, although it exist user_id in likes array from replies array embed.
How to fix it? Thanks!
It looks like your post is mostly code; please add some more details.
repliesin your document as shown is an array, and therefore simple "dot notation" in a$projectstage is not enough in order to alter every array element. If you expect all array elements to be altered then you must use$map