Adding a status key to post collection to indicate if a user has liked the post with find() method

Hello, I need to retrieve a post to populate a user field and also determine if a user has liked the post.
for clarification, I have a post, user, and likes collection

likes collection holds a reference to the user id and post id.

I want to retrieve a list of posts and check if the user has liked the post or not (Restful API) which is necessary for the user interface

please, does anyone knows the best way to go about this?

Hi @EmeritusDeveloper,

Have you considered using $lookup? If that doesn’t suit your use case, please provide the following information:

  1. Sample documents from the post, user, and likes collections.
  2. Expected output
  3. MongoDB version in use

Additionally, i’ve noticed you’ve advised “with find() method” in your post title - Is there a specific reason you’re wanting to do this with find() rather than perhaps .aggregate() for example?


1 Like