I have a neo4j database that stores forum posts. Replies to specific posts are encoded using a replyto relationship. I am trying to retrieve all the information from the database, convert the relationships into node properties, then output all the nodes with the new replyto property in JSON format. I think the replyto property in the JSON object should be an array of all the post IDs that are replies to the given post, but please suggest something better if you think of it.

What cypher query should I use to retrieve this data and how should I convert the relationships into node properties using javascript?


The cypher query that ended up working for me was MATCH (n) OPTIONAL MATCH (n)-[r]->(m) RETURN n,r,m

function makeAllBlocksQuery(cb){
    query: "MATCH (n) OPTIONAL MATCH (n)-[r]->(m) RETURN n,r,m",
  }, function(err,results){
    var processedResults = handleAllBlocks(err, results);

function handleAllBlocks(err, results) {
    if (err) throw err;
    if (!results) {
      console.log('No blocks found.');
      return [];
    } else {
      //first, form [_fromId, _toId] relationshipArray
      var relationshipArray = new Array();
      for (count = 0; count < Object.keys(results).length; count++) {
        if(results[count].r !== null) {
          var relationship = [results[count].r._fromId, results[count].r._toId];

      var resultArray = new Array();
      var repliedIdsArray = new Array();
      //add nodes found in results to resultArray
      //and add id property to properties for later
      for (count = 0; count < Object.keys(results).length; count++) {
        results[count].n.properties.id = results[count].n._id;

        //iterate through the relationshipArray for each result
        //and add all the replied Ids to the repliedIdsArray
        for (relationshipcount = 0; relationshipcount < relationshipArray.length; relationshipcount++){
          if (relationshipArray[relationshipcount][1] == results[count].n.properties.id) {
        //add the repliedIdsArray to the result's properties
        results[count].n.properties.hasreplyto = repliedIdsArray;
        repliedIdsArray = [];
        //stringify so it can be sent to client more easily
      return resultArray;
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.