Neo4j 插入具有各种关系的节点
Neo4j insert nodes with various relationships
我尝试创建 "recipe"、"ingredients" 和 "user" 节点(作者)。
基本上一个作者可以有很多菜谱;并且食谱与配料具有多对多关系。
我有这个问题:
MATCH (user:User)
WHERE ID(user) = {uid}
CREATE (recipe:Recipe {name:"receta 4"})
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(7)
CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]-(ingredient)
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(6)
CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]-(ingredient)
CREATE (user)-[:PREPARE]->(recipe)
RETURN recipe
但是,我得到错误:
ingredient not defined (line 4, column 7)
"WITH (ingredient:Ingredient)"
^
Neo.ClientError.Statement.InvalidSyntax
这个查询的正确格式是什么?
我的js代码:
Recipe.create = function (req, callback) {
var data = req.body;
var uid = parseInt(req.params.id);
var query = [
'MATCH (user:User)',
'WHERE ID(user) = {uid}',
'CREATE (recipe:Recipe {data})',
];
// set ingredients
var ingId;
var unit;
var cant;
for (var i = data.ingredients.length - 1; i >= 0; i--) {
ing = data.ingredients[i];
ingId = parseInt(ing.id);
unit = ing.unit;
cant = ing.cant;
query.push(
'MATCH (ingredient:Ingredient)',
'WHERE ID(ingredient) = node(' + ingId + ')',
'CREATE (recipe)-[ri:HAS {unit: "'+unit+'", cant: '+cant+'}]-(ingredient)'
);
}
query.push(
'CREATE (user)-[:PREPARE]->(recipe)',
'RETURN recipe'
);
query.join("\n");
db.cypher({
query:query,
params:{
data: data,
uid: uid
}
}, function (err, results) {
if (err) return callback(err);
console.log(results)
callback(null, results);
});
};
我认为问题在于引用下一个要创建的节点的 WITH 子句,而不是引用要创建的前一个节点。此外,代码正在尝试创建无向关系。
试试这个:
MATCH (user:User)
WHERE ID(user) = {uid}
CREATE (recipe:Recipe {name:"receta 4"})
WITH recipe
MATCH (ingredient:Ingredient)
WHERE id(ingredient) = 7
CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]->(ingredient)
WITH recipe
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = 6
CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]->(ingredient)
CREATE (user)-[:PREPARE]->(recipe)
RETURN recipe
我尝试创建 "recipe"、"ingredients" 和 "user" 节点(作者)。
基本上一个作者可以有很多菜谱;并且食谱与配料具有多对多关系。
我有这个问题:
MATCH (user:User)
WHERE ID(user) = {uid}
CREATE (recipe:Recipe {name:"receta 4"})
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(7)
CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]-(ingredient)
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(6)
CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]-(ingredient)
CREATE (user)-[:PREPARE]->(recipe)
RETURN recipe
但是,我得到错误:
ingredient not defined (line 4, column 7)
"WITH (ingredient:Ingredient)"
^
Neo.ClientError.Statement.InvalidSyntax
这个查询的正确格式是什么?
我的js代码:
Recipe.create = function (req, callback) {
var data = req.body;
var uid = parseInt(req.params.id);
var query = [
'MATCH (user:User)',
'WHERE ID(user) = {uid}',
'CREATE (recipe:Recipe {data})',
];
// set ingredients
var ingId;
var unit;
var cant;
for (var i = data.ingredients.length - 1; i >= 0; i--) {
ing = data.ingredients[i];
ingId = parseInt(ing.id);
unit = ing.unit;
cant = ing.cant;
query.push(
'MATCH (ingredient:Ingredient)',
'WHERE ID(ingredient) = node(' + ingId + ')',
'CREATE (recipe)-[ri:HAS {unit: "'+unit+'", cant: '+cant+'}]-(ingredient)'
);
}
query.push(
'CREATE (user)-[:PREPARE]->(recipe)',
'RETURN recipe'
);
query.join("\n");
db.cypher({
query:query,
params:{
data: data,
uid: uid
}
}, function (err, results) {
if (err) return callback(err);
console.log(results)
callback(null, results);
});
};
我认为问题在于引用下一个要创建的节点的 WITH 子句,而不是引用要创建的前一个节点。此外,代码正在尝试创建无向关系。
试试这个:
MATCH (user:User) WHERE ID(user) = {uid} CREATE (recipe:Recipe {name:"receta 4"}) WITH recipe MATCH (ingredient:Ingredient) WHERE id(ingredient) = 7 CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]->(ingredient) WITH recipe MATCH (ingredient:Ingredient) WHERE ID(ingredient) = 6 CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]->(ingredient) CREATE (user)-[:PREPARE]->(recipe) RETURN recipe