Create group only if there is no group with same name

10 views
Skip to first unread message

Johnny Luu

unread,
May 29, 2012, 2:34:19 PM5/29/12
to Neo4j Google Groups
I'm trying to create a Cypher query that creates a group only if there is no group with similar name.

But I'm pretty clueless about how to do this.

How could I check this in the query?

Johnny

Sent from my iPad

Mattias Persson

unread,
May 29, 2012, 3:36:54 PM5/29/12
to ne...@googlegroups.com
What is a "group", just a node with a "name" property?

2012/5/29 Johnny Luu <johnny....@gmail.com>



--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

Johnny Weng Luu

unread,
May 29, 2012, 3:48:28 PM5/29/12
to ne...@googlegroups.com
Yes. Forgot to mention that :)

Johnny

Mattias Persson

unread,
May 29, 2012, 4:03:41 PM5/29/12
to ne...@googlegroups.com
From what I know RELATE does that. So let's say:

  START groups=node(10)
  RELATE groups-[:GROUP]->(group {name:'Mattias'})

that will create the group "Mattias" if it doesn't already exist, i.e. if there isn't already a node connected via a GROUP relationship with the property name:'Mattias'.
Also look at http://docs.neo4j.org/chunked/milestone/query-relate.html

2012/5/29 Johnny Weng Luu <johnny....@gmail.com>

Johnny Weng Luu

unread,
May 29, 2012, 4:21:02 PM5/29/12
to ne...@googlegroups.com
Yeah that was something I did not know.

But it seems that all the data in group has to be identical, or it will create a new one.

The other properties could be different, I just want to check that the name property is not identical.

Seems like I have to make a CHECK query and then do the IF logic in application server to determine if I should make a CREATE query.

Thanks for the help.

Johnny

Mattias Persson

unread,
May 29, 2012, 4:32:10 PM5/29/12
to ne...@googlegroups.com
I'm assuming you don't have to specify all the properties that the group has if it exists to "identify" it. Whatever properties you write within the {key:value} bracket will be considered a match. So even if you have a group with the properties name, age, and type (just some random properties I made up) and do this RELATE query which matches on name only, that group will be identified and considered existing so a new one wouldn't be created.

Am I right Andrés (or any other well travelled Cypher guy)?

Johnny Weng Luu

unread,
May 29, 2012, 4:43:36 PM5/29/12
to ne...@googlegroups.com
I just tested it on the console and it worked as you expected.

This was a really clean solution for me!

Problem solved, thanks :D

Johnny
Reply all
Reply to author
Forward
0 new messages