3

Let consider this collection of posts. Each post has an array of comments and each comments has an array on string, with the key 'likes', which indicates the users who like the comment.

{
_id: 000
created_at:
user_id:
comments: [{
       _id: 111
       created_at:
       likes: [user_id1 , user_id2 , user_id3]
       },
       {
       _id: 222
       created_at:
       likes: [user_id1]
       },]
}

How can I check, with mongoose, if a user has liked a comment with a given id?

1 Answer 1

3

I think that this query may be the thing you're looking for...

{
  comments: {
    $elemMatch: {
      _id: 111,
      likes: user_id1
    }
  }
}

Hope it helps you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.