cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Passing the $id in as a variable to find a relationship or node

I'm trying to make a simple deleteRelationship in Javascript where it can take in a relationship id. Something simple like this-

MATCH (a)-[r]-(b) WHERE id(r)=$id DELETE r

Unfortunately, if I hardcode the $id, it works, but if I pass it in as a variable, I don't get a record. Both are number types, but it still seems to not be finding the relationship.

1 ACCEPTED SOLUTION

You all got me on the right track though. toInt worked for this part of my query. Looks like it was constantly changing it to floats if in JS you just use a basic number format.

const DELETE_RELATIONSHIPS = `
  UNWIND $relationshipIds AS relationshipId
  MATCH (a)-[r]-(b)
  WHERE id(r)=toInt(relationshipId)
  DELETE r
`;

View solution in original post

7 REPLIES 7

You can use :param id => 12345 in browser to set a parameter.

Is the ID parameter converted to a neo4j int? neo4j.int(100)

Hi Thomas,

You can use an Array like this.

:param id => [7,3,2]

MATCH (a)-[r]->(b)
WHERE id(r) IN $id
DELETE r

Sorry, I was saying that if you pass it in using a JS function. Shouldn't have mentioned being able to hardcode it in the browser. Here is the long version that won't find the relationship ID that clearly exists in the browser and works if I hardcode it.

huum.deleteRelationships([3215630])

const deleteRelationships = relationshipIds => {
  return new Promise(async function(resolve, reject) {
    try {
      const idsToDelete = { relationshipIds: relationshipIds };
      let queryResult = await runQuery(DELETE_RELATIONSHIPS, idsToDelete);
      resolve(queryResult);
    } catch (err) {
      reject(err);
    }
  });
};
const runQuery = (query, params) => {
  return new Promise(function(resolve, reject) {
    const session = driver.session();
    session
      .run(query, params)
      .then(function(result) {
        console.log("Success ", query);
        session.close();
        driver.close();
        resolve(result);
      })
      .catch(function(error) {
        console.log("Error running query!", error);
        session.close();
        driver.close();
        reject(error);
      });
  });
};
const DELETE_RELATIONSHIPS = `
  UNWIND $relationshipIds AS relationshipId
  MATCH (a)-[r]-(b)
  WHERE id(r)=relationshipId
  DELETE r
`;

You all got me on the right track though. toInt worked for this part of my query. Looks like it was constantly changing it to floats if in JS you just use a basic number format.

const DELETE_RELATIONSHIPS = `
  UNWIND $relationshipIds AS relationshipId
  MATCH (a)-[r]-(b)
  WHERE id(r)=toInt(relationshipId)
  DELETE r
`;

Hi Nick,

JS only has numbers (with a decimal), so if you pass in something that looks like an Int, you need to convert it as you have shown. This isn't Neo4j behavior per se, it's js converting it to a float while you weren't looking , then it no longer matches.

David

I would convert it to a "neo4j int" in JS using neo4j.int(integer).