How To Perform CRUD Operation In NODEJS with neo4j

Posted By : Vipul Pandey | 20-Apr-2018

In the previous bog be learned how to connect neo4j with nodejs and in this blog we will learn how to save and retrieve data from database and store in the database using cypher queries.
1 SAVE : To store the data in neo4j we need to use create a statement of neo4j which creates the specified labeled node in the database and set the respective property in the node, eg if we need to create the node which stores the movie data then we need to use the label movie or as desired and set parameter in indexed order.

   session
    .run("CREATE (movie:Movie{name:{name},releaseDate:{releaseDate}})  RETURN movie",{name:"MY MOVIE",releaseDate:"22-04-18"})
      .then(function (result) {
                console.log(result);
       })
       .catch(function (error) {
                console.log(error);
       });
    

2 GET: To get the data from neo4j using bolt driver we need to use GET  statement of neo4j which takes the name of the label to search the specified labeled nodes in the database and return the respective properties of the nodes.

   session
        .run("MATCH (movie:Movie)  RETURN movie")
            .then(function (result) {
                console.log(result);               
            })
            .catch(function (error) {
              console.log(error);
   });

Note if we don't mention the label then it will return all the nodes of the database.

3. UPDATE:  To update any nodes in the database we need to use MATCH statement of neo4j and set the respective properties in the node. 

  session
     .run('MATCH (movie:Movie) where id(movie)={id} set movie.name={name} return movie', { id:121,name:"MOVIE-2" })
        .then(function (result) {
            console.log(result);
          })
         .catch(function (error) {
            console.log(error);
    });

Note: The searching of the nodes will be faster when we mention the label of the node.

4 DELETE: To delete any nodes in the database we need to use the DELETE statement of the neo4j. If the nodes are attached to any other node with a relationship then we must need to detach the node from a relationship before delete.

  session
     .run('MATCH (movie:Movie) where id(movie)={id} detach delete movie', { id: 121 })
        .then(function (result) {
             console.log(result);
         })
        .catch(function (error) {
         console.log(error);
   });

About Author

Author Image
Vipul Pandey

Vipul Pandey is a good team-player & developing application on MEAN and java spring boot. Vipul loves to keep rotating fingers in his keyboard until he creates somethings inspiring.Hobbies are playing cricket ,swimming & photography.

Request for Proposal

Name is required

Comment is required

Sending message..