ArangoDB中如何查询同一人及周星驰主演并执导的影片?
Let's break down your two questions with practical AQL examples, tailored to your graph structure.
1. Query all movies where the same person both starred in and directed them
To find films where a single individual holds both the actor and director roles, we can leverage ArangoDB's graph traversal and edge existence checks. Here are two efficient approaches:
Approach 1: Start from movies, verify dual roles
This method checks each movie to see if there's a star who both acted in and directed it:
FOR movie IN Movie FOR star IN Star // Check if the star has both a "star_in" and "director" edge to the movie FILTER EXISTS(EDGE(star, movie, "star_in")) AND EXISTS(EDGE(star, movie, "director")) RETURN { movie_title: movie.title, multi_role_person: star.name }
Approach 2: Start from stars, find overlapping movies
If you have fewer star nodes than movie nodes, this might be more performant. We first get all movies a star acted in, then filter for ones they also directed:
FOR star IN Star // Get all movies the star acted in FOR star_edge IN star_in FILTER star_edge._from == star._id // Check if the same star directed that movie FOR dir_edge IN director FILTER dir_edge._from == star._id AND dir_edge._to == star_edge._to RETURN { movie_title: DOCUMENT(star_edge._to).title, multi_role_person: star.name }
2. Query movies starring and directed by Stephen Chow (after adding the producer edge)
Adding the producer edge doesn't affect this query since we're only focusing on star_in and director relationships. Here's how to target Stephen Chow specifically:
Concise version using node lookup and edge checks
// First, get Stephen Chow's star node LET stephen_chow = FIRST(FOR s IN Star FILTER s.name == "Stephen Chow" RETURN s) // Find all movies where he's both actor and director FOR movie IN Movie FILTER EXISTS(EDGE(stephen_chow, movie, "star_in")) AND EXISTS(EDGE(stephen_chow, movie, "director")) RETURN movie.title
Alternative using edge traversal
If you prefer working directly with edge collections:
LET stephen_chow = FIRST(FOR s IN Star FILTER s.name == "Stephen Chow" RETURN s) FOR star_edge IN star_in FILTER star_edge._from == stephen_chow._id // Match the same movie in the director edge collection FOR dir_edge IN director FILTER dir_edge._from == stephen_chow._id AND dir_edge._to == star_edge._to RETURN DOCUMENT(star_edge._to).title
Both queries will return only the movies where Stephen Chow is credited as both actor and director, ignoring the new producer edge entirely (since we don't include it in our filters).
内容的提问来源于stack exchange,提问作者mianbaojun

