Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

neo4j - Creating a relationship between all nodes with a set property with Cypher

First of all, I'm a complete newbie with neo4j and cypher so I apologize if this is a dumb question. Right now I'm not creating any actual database, I'm just practicing in a sandbox with silly hypotethical databases I come up with to practice, then try to formulate queries. My problem is the following: let's say I have a database with a number of animals, each having the property "class"

MERGE (a:Animal {name: 'cat'})
ON CREATE SET a.class = 'mammal'
MERGE (a:Animal {name: 'lizard'})
ON CREATE SET a.class = 'reptile'
MERGE (a:Animal {name: 'ant'})
ON CREATE SET a.class = 'insect'

Now let's say I want to know which of these animals has four limbs based on their class. Is there a way for me to tell the the graph I want to add the property

a.limbs = 4

to every animal that already has the property "mammal" and "reptile" (let's pretend snakes don't exist), and not to a single node? And if I can do that, what would the query which could be interpreted as "show me every animal who has four limbs" be?

Many thanks

question from:https://stackoverflow.com/questions/65928424/creating-a-relationship-between-all-nodes-with-a-set-property-with-cypher

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure, you can do:

MATCH (a:Animal)
WHERE a.class IN ["mammal","reptile"]
SET a.limbs=4

To query all animals with 4 limbs:

MATCH (a:Animal {limbs:4}) 
RETURN a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...