Reputation: 1002
Im working on some sort of social network in which people are able to make posts about a topic and like them.
Im having trouble tracking user likes.
The schema is the following:
Users: { userId: "someId", likes: ["idPost1", "idPost4", ...] }
Posts: { postId: "someId", topic: "idTopic", postContent: "someContent"}
I need a query that can:
Take all posts from a certain topic, like this:
r.table('posts').filter({ topic: idTopic }).run().then( posts => res.json(posts))
Look up to see if the current user (given by the user id) has liked any of the posts on that specific topic. Then return a JSON with all posts on that topic, and those liked by the user with "liked: true".
Im having trouble with step 2,
Please let me know if im modelling data the wrong way, or if you can think of any way I can accomplish step 2.
Thanks!
Upvotes: 0
Views: 48
Reputation: 367
This function does what you're looking for in javascript. (Tested using the Data Explorer)
r.db("test").table('posts').filter({ topic: "idTopic" }).merge(function(post){
return {liked: r.table('users').filter({userId: "someId"})(0)('likes').contains(post("postId")) }; })
The merge function basically iterates every post and adds the field liked to it. The contains function checks whether the posts id is in the likes array and return in directly as truth value.
Upvotes: 0