# Delete all "person" vertices withlocation equal to"us"CREATE QUERY deleteEx() FOR GRAPH workNet { S = {person.*};DELETE s FROM S:sWHERE s.locationId =="us";}
DELETE 语句示例2
# Delete all "worksFor" edges where the person's location is "us"CREATE QUERY deleteEx2() FOR GRAPH workNet { S = {person.*}; DELETE e FROM S:s -(worksFor:e)-> company:t WHERE s.locationId == "us";}
# Remove any post vertices posted by the given userCREATE QUERY deletePosts(vertex<person> seed) FOR GRAPH socialNet {start= {seed};# Best practice istodelete a vertex in a POST-ACCUM, which only# occurs once for each vertex v, guaranteeing that a vertex isnot# deleted more than oncepostAccumDeletedPosts =SELECT v FROMstart-(posted:e)-> post:v POST-ACCUM DELETE (v);# Possible, but not recommended as the DML-sub DELETEstatement occurs# once for each edge of the vertex vaccumDeletedPosts =SELECT v FROMstart-(posted:e)-> post:v ACCUM DELETE (v);}# Need a separate query to display the results, because deletions don't take effect during the query.CREATE QUERY selectUserPosts(vertex<person> seed) FOR GRAPH socialNet { start = {seed}; userPosts = SELECT v FROM start -(posted:e)-> post:v; PRINT userPosts;}
例如,如果顺序执行以下的selectUserPosts和deletePosts查询
deletePosts.run
RUN QUERY selectUserPosts("person3")RUN QUERY deletePosts("person3")RUN QUERY selectUserPosts("person3")
会得到以下结果:
DeletePosts 示例的结果
# Before the deletion{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": [{"selectedPosts": [{"v_id": "2","attributes": {"postTime": "2011-02-03 01:02:42","subject": "query languages" },"v_type": "post" }]}]}# Deletion; nooutput results requested at this point{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": []}# After the deletion{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": [{"selectedPosts": []}]}
CREATE QUERY insertEx(STRING name, STRING name2, STRING name3, STRING comp) FOR GRAPH workNet {# Vertex insertion# Adds 2'company' vertices. One is located in the USA, and a sister company in Japan.INSERT INTO company VALUES ( comp, comp, "us" );INSERT INTO company (PRIMARY_ID, country) VALUES ( comp +"_jp", "jp" );# Edge insertion# Adds a 'worksFor' edge from person 'name'to the company 'comp', filling indefault# valuesfor startYear (0), startMonth (0), and fullTime (false).INSERT INTO worksFor VALUES (name person, comp company, _, _, _);# Adds a 'worksFor' edge from person 'name2'to the company 'comp', filling indefault# valuesfor startMonth (0), but specifying valuesfor startYear and fullTime.INSERT INTO worksFor (FROM, TO, startYear, fullTime) VALUES (name2 person, comp company, 2017, true);# Adds a 'worksFor' edge from person 'name3'to the company 'comp', filling indefault# valuesfor startMonth (0), and fullTime (false) but specifying a valuefor startYear (2017).INSERT INTO worksFor (FROM, TO, startYear) VALUES (name3 person, comp company, 2000+17);}
# Add a child company of a given company name. The new child company isin japanCREATE QUERY addNewChildCompany(STRING name) FOR GRAPH workNet { allCompanies = {company.*}; X =SELECT sFROM allCompanies:sWHERE s.id ==name ACCUM INSERT INTO company VALUES ( name+"_jp", name+"_jp", "jp" );}# Add separate query to list the companies, beforeandafter the insertionCREATE QUERY listCompanyNames(STRING countryFilter) FOR GRAPH workNet { allCompanies = {company.*}; C =SELECT sFROM allCompanies:sWHERE s.country == countryFilter;PRINT C.size() AS numCompanies;PRINT C;}
# Change all "person" vertices withlocation equal to"us"to"USA"CREATE QUERY updateEx() FOR GRAPH workNet { S = {person.*};UPDATE s FROM S:sSET s.locationId ="USA", # simple base type attribute s.skillList = [1,2,3] # collection-type attributeWHERE s.locationId =="us";# The update cannot become effective within this query, so PRINT S still show "us".PRINT S;}
# The second example is equivalent to the above updateExCREATE QUERY updateEx2() FOR GRAPH workNet { S = {person.*}; X =SELECT sFROM S:sWHERE S.locationId =="USA" POST-ACCUM S.locationId ="us", S.skillList = [3,2,1];PRINT S;}
# change the given person's new locationCREATE QUERY updateByAssignment(VERTEX<person> v, STRING newLocation) FOR GRAPH workNet{ v.locationId = newLocation;}