Sunday, 19 March 2017

What's the most efficient way to check if element exists in a set while also querying the document containing the set in NodeJs and MongoDB?

so in my MongoDB database I have a collection holding user posts.

Within that collection I have a set called "likes", which holds an array of the ids of the users that have liked that post. When querying I would like to pass a user id to my query and have a boolean in the result telling me whether the id exists in the array to see whether the user has already liked the post. I understand this would be easy to do with two queries, one to get the post and one to check if the user has liked it, but I would like to find the most efficient way to do this.



via Duxducis

No comments:

Post a Comment