cancel
Showing results for 
Search instead for 
Did you mean: 

create unique node

hello 

im trying create node with 1 unique property called name so i try create something like this

CREATE CONSTRAINT ON (n:words) assert n.name is unique
and after that
create (n:words{name:"test"})
so my problem is i dont want to be case sensitive i mean for example if we send word and Word it doesn't create the second one 
and next thing i wanna ask is when i use above query it says this feature is deprecated so is there any new way? and because i wanna use this on node js is it problem to repeat the first query?
1 ACCEPTED SOLUTION

You should pass the whole list to your query and UNWIND it like this:

 

UNWIND $words AS item
MERGE (n:Word {name: toLower(item)})

 

View solution in original post

5 REPLIES 5

Cobra
Ninja
Ninja

Hello @hoseinmorshedy 😊

If you are on the latest version of Neo4j (4.4.x):

CREATE CONSTRAINT constraint_Word IF NOT EXISTS FOR (n:Word) REQUIRE n.name IS UNIQUE;

If you create a unique constraint, then you should use MERGE clause instead of CREATE:

MERGE (n:Word {name: toLower($your_string)})

Regards,

Cobra

but how we make it uncase sensitive?

i dont want to create 2 node called A and a

 

You can't do it directly, that's why I used the toLower() function to make sure it creates only one node.

one more thing in node js im trying to get all index of array with foreach and then merge it into neo4j the problem i have is it just merge 1 item into database my code is:

words.forEach((res, index) => {
session.run(`merge (words{name:'${res}'})`)
}

You should pass the whole list to your query and UNWIND it like this:

 

UNWIND $words AS item
MERGE (n:Word {name: toLower(item)})

 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.