0
$\begingroup$

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?

$\endgroup$

closed as off-topic by Toros91, Stephen Rauch, Elias Strehle, Dawny33 Mar 10 '18 at 17:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about data science, within the scope defined in the help center." – Toros91, Stephen Rauch, Dawny33
If this question can be reworded to fit the rules in the help center, please edit the question.

0
$\begingroup$

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

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

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];
          relationshipArray.push(relationship);
        }
      }

      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) {
            repliedIdsArray.push(relationshipArray[relationshipcount][0]);
          }
        }
        //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
        resultArray.push(JSON.stringify(results[count].n.properties));
      }
      return resultArray;
    }
}
$\endgroup$

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