Welcome to OStack Knowledge Sharing 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...