简体   繁体   中英

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

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

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