I have 2 vertexes one is USER and one is PLACE, Any user can review a place and any user can like or comment on that review. In this scenario, there will be two edges one storing review done by any user on any place and one storing any kind of activity done on that review. Suppose I need to fetch all reviews at any place with like and comment counts of each review in a single query, how do I write such query.
Asked
Active
Viewed 73 times
1 Answers
0
Assuming that you are using user and place to store your vertices and edges activity, review with labels comment, like or review to store the activity you could use graph traversals with depth 1, that are using all vertices of your place collection as starting vertices.
The following query iterates through all place documents and returns likes, comment and reviews for each one.
FOR vertex IN place
LET likes = LENGTH (FOR v, e, p IN 1..1 ANY vertex review, activity FILTER p.edges[0].label == 'like' RETURN 1)
LET reviews = (FOR v, e, p IN 1..1 ANY vertex review, activity FILTER p.edges[0].label == 'review' RETURN p.edges[0].rv)
LET comments = LENGTH (FOR v, e, p IN 1..1 ANY vertex review, activity FILTER p.edges[0].label == 'comment' RETURN 1)
RETURN {place:vertex.name, likes:likes, reviews: reviews, comments: comments}
Filters specify the activity of the traversed edge. The amount of edges with label like/comment represent the amount of likes/comments for this place while the reviews traversal returns the review with attribute rv that is saved in the edge.

Maximilian Kernbach
- 571
- 2
- 7
-
I think this query will not give the count of likes or comments on each review, With this query, we will get all reviews, all comment count, and all like count. My scenario is different I need to fetch all reviews at a place with like and comment count on that review. Hope I made my question clear. – Mustanish Altamash Jun 25 '18 at 09:13
-
This query returns all reviews, likes and comments for each place, not all likes and comments for each review of each place. It might be useful to have the structure user -> review -> place to match your scenario. This way you could use two separate traversals with depth one: The first one using the place collection would return all reviews that are referring to this place (edges pointing from review to place), while the second traversal would return all comments and likes for this review (edges pointing from user to review with label like/comment). – Maximilian Kernbach Jun 26 '18 at 09:23
-
Is this query good enough or optimized enough `FOR p IN places FILTER p.place_id == "MMI000" FOR vertex, edge, path IN 1..1 INBOUND p relation LET likeCount = LENGTH(FOR ivertex, iedge, ipath IN 1..1 INBOUND vertex relation FILTER iedge.type == 'like' RETURN 1) LET flagCount = LENGTH(FOR ivertex, iedge, ipath IN 1..1 INBOUND vertex relation FILTER iedge.type == 'flag' RETURN 1) RETURN {likeCount:likeCount, review: DOCUMENT(vertex._id), flagCount:flagCount}`. If not then please suggest – Mustanish Altamash Jun 26 '18 at 09:58