hi all, I am very new to mongodb and got stuck to a problem.
i have a collection as shown below
{
"_id" : ObjectId("5881b26f2694efd0ef17eb8b"),
"firstName" : "sumit",
"lastName" : "vishwakarma",
"likes" : [
{
"id" : 1,
"value" : "java"
},
{
"id" : 2,
"value" : "c++"
}
]
}
{
"_id" : ObjectId("5881baf82694efd0ef17eb8c"),
"firstName" : "test",
"lastName" : "test",
"likes" : [
{
"id" : 3,
"value" : "java"
},
{
"id" : 2,
"value" : "c++"
}
]
}
i want to write a query which give members and search for a particular id in likes and if that id is present then it should be true else false.
below is how the output should look
search for id:3 in likes
{
"_id" : ObjectId("5881b26f2694efd0ef17eb8b"),
"firstName" : "sumit",
"lastName" : "vishwakarma",
"likes" : [
{
"id" : 1,
"value" : "java"
},
{
"id" : 2,
"value" : "c++"
}
] ,
"isLiked":false
}
{
"_id" : ObjectId("5881baf82694efd0ef17eb8c"),
"firstName" : "test",
"lastName" : "test",
"likes" : [
{
"id" : 3,
"value" : "java"
},
{
"id" : 2,
"value" : "c++"
}
],
"isLiked":true
}
any hint will be appreciated
What I have tried:
i have tried writing query using aggregation, $elemMatch but couldn't get the desired output