bookmark a post and I defined a field in post schema like the following:
bookmarks: [{type: String, ref:'User'}]
and I have store the id of the user in this field.
Now I want to get bookmarked post of a user.
For example I want to know, user with id 12 bookmarked witch posts?
How can I do this in mongoose?
now, I write the following code and it's working but I think it's not the best solution const posts = await PostModel.find({}) const bookmarkedPosts = posts.filter((post) => post.bookmarks.includes(req.user._id)) return res.status(200).json({ data: bookmarkedPosts })
This is not how to do relations
You should have: UserSchema PostSchema BookmarkSchema BookmarkSchema could for example contain: date - when the bookmark was made UserId - the user that bookmarked the post PostId - the post that was bookmarked
Обсуждают сегодня