I have this collection:
{
"_id": "5f1bd6c7d90cb03e845adbbf",
"name": "John Doe",
"likes": [
{
"_id": "5f378b105d337347e8958a50",
"user": "5f357ac9c0d2019cc82b5a2f",
"post": "5f3032894cd4a000175d333c",
},
{
"_id": "5f354e351a98dd7c7081d72d",
"user": "5f357ac9c0d2019cc82b5a2f",
"post": "5f3bf7a44ed4f031bc575c2b"
},
{
"_id": "5f354cc2879f8a81b0ab28fe",
"user": "5f30380288a63e001755401e",
"post": "5f310ce5e18bad001792fb60"
}
]
Now I want to find all posts that match the IDs of likes.post.
This works but obviously only gives me one post:
const posts = await Post.find({ _id: [user.likes[0].post] });
res.json(posts);
I tried a for-loop but it only gives back one post. When I console log this I get all posts that match:
for (let i = 0; i < user.likes.length; i++) {
const posts = await Post.find({ _id: [user.likes[i].post] });
res.json(posts);
}