简体   繁体   中英

Arango query, collection with Edge count

Really new to Arango and I'm experimenting with it, so please bear with me. I have a feed collection and every feed can be liked by a user

[user]---likes_feed--->[feed].

I'm trying to create a query that will return a feed by its author, and add the number of likes to the result. This is what I have so far and it seems to work, but it only returns feed that have at least 1 like (An edge exists between the feed and a user) below is my query

FOR f IN feed  
SORT f.creationDate  
FILTER f.author == @user_key 
LIMIT @start_index, @end_index
FOR x IN INBOUND CONCAT('feed','/',f._key) likes_feed 
OPTIONS {bfs: true, uniqueVertices: 'global'} 
COLLECT feed = f WITH COUNT INTO counter  
RETURN {   
'feed':feed,
likes: counter  
}

this is an example of result

[
"feed":{
"_key":"string",
"_id":"users_feed/1680835", 
"_rev":"_W8zRPqe--_",
"author":"author_a",
"creationDate":"98879845467787979",
"title":"some title",
"caption":"some caption'
},
"likes":1
]

If a feed has no likes, no edge inbound to that feed, how do I return the likes count as 0

Something like this?

[
"feed":{
"_key":"string",
"_id":"users_feed/1680835",
"_rev":"_W8zRPqe--_",
"author":"author_a",
"creationDate":"98879845467787979",
"title":"some title",
"caption":"some caption'
},

]

So finally I found the solution. I had to create a graph and traverse it. Final result below
FOR f IN users_feed
SORT f.creationDate
FILTER f.author == @author_id
LIMIT @start_index, @end_index
COLLECT feed = f
LET inboundEdges = LENGTH(FOR v IN 1..1 INBOUND feed GRAPH 'likes_graph' RETURN 1)
RETURN {
feed :feed
,likes: inboundEdges
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM