Reputation: 11
The question is just as the title, and the cypher statement is just like the follow:
Match (n: test) CREATE (copy : LABELS(n)) set copy = n
Its purpose is to create a node with the same properties and same labels as the other node, but it doesn't work now because we cannot use a expression like LABELS(n)
to set lobel to a node.
How can I make it work?
Upvotes: 1
Views: 342
Reputation: 6251
You could get the node's properties and labels you want to copy and then dynamically create another cypher statement that you execute.
Using the transactional api, it could look like this:
// requires cypher-rest
// npm i cypher-rest
'use strict';
const util = require('util');
const c = require('cypher-rest');
const neoUrl = 'http://127.0.0.1:7474/db/data/transaction/commit';
const copyNode = propertyObjMatch => {
return new Promise((resolve, reject) => {
// find node(s) via property matching and return it(/them)
const cypher = `MATCH (x ${util.inspect(propertyObjMatch)}) RETURN DISTINCT x, LABELS(x) AS labels`;
return c.run(cypher, neoUrl, true) // third parameter set to true to always return a list of results
.then(results => {
// iterate over results and create a copy one by one
results.forEach(result => {
const copy = `CREATE (copy:${[...result.labels].join(':')}) SET copy = ${util.inspect(result.x)} RETURN copy`;
c.run(copy, neoUrl);
});
})
});
};
// create a node
c.run('CREATE (x:LABEL1:LABEL2 {withProp: "and value", anotherProp: "test"}) RETURN x', neoUrl).then(() => {
copyNode({withProp: 'and value', anotherProp: 'test'})
.then(console.log)
});
Please excuse the hackiness, but it should bring the point across.
Upvotes: 0
Reputation: 67044
Unfortunately, labels currently cannot be set directly from data values.
Upvotes: 1