我们如何在一个密码中将标签从一个节点复制到另一个节点?

时间:2016-02-04 03:07:52

标签: neo4j cypher

问题就像标题一样,而cypher声明就像下面这样:

Match (n: test) CREATE (copy : LABELS(n)) set copy = n

它的目的是创建一个与另一个节点具有相同属性和相同标签的节点,但它现在不起作用,因为我们不能使用像LABELS(n)这样的表达式来设置节点。

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:1)

不幸的是,目前无法直接从数据值设置标签。

答案 1 :(得分:0)

您可以获取要复制的节点属性和标签,然后动态创建您执行的另一个cypher语句。

使用事务性api,它可能如下所示:

// 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)
});

请原谅黑客,但它应该说明问题。

相关问题