Deepak Garg
Deepak Garg

Reputation: 83

Neo4j query with nodejs

I have an array of 1000 cypher (neo4j) queries (in string form).

when I loop (for loop, individual values) through this array in session, all queries are going in sequence. Though I receive 1000 results back also, however, the result of order is changed.

how can I synchronize them so that I get a result in the order, as the queries in an array?

...............................................

for example a =[t1,t2,t3,t4...]

the result from cypher can be in any order say t2,t1,t4,t3

I want the result in same t1, t2, t3, t4 manner

Any suggestion, please?

Upvotes: 1

Views: 931

Answers (2)

Gaurav
Gaurav

Reputation: 103

Are you trying to store the data and does all the cypher perform same operation with same attrib and props? If yes, then why don't you use the feature of unwind, the job will be done in one cypher that too in only one call. The return data will be in the sequence you want. Even if there are different types of cyphers, group them and use the unwind, this result in less number of session(almost 1) as well as connection which result in increase in performance that too producing result in less time as compared to executing one cypher at a time.

Go through this post as it will definitely help you.

If you are not storing data, then either you can use promises concept of node.js or can create a coding structure as follows:

let cypher_array=[{cypher:'you cypher',param:'param'}]
let counter = 0;
function execute_cypher(counter) {
    if (counter < cypher_array.length) {
        session
        .run(cypher_array[counter].cypher, cypher_array[counter].param)
        .then(function (result) {
            //you logic here


            counter++;
            if(counter < cypher_array.length){
                execute_cypher(counter)
            }
        })
        .catch(function (error) {
            if(counter < cypher_array.length){
                execute_cypher(counter)
            }
            //comment the above code if you do not want to continue when error is occurred
        })
    }else{
        console.log('completed!');
        session.close();
        driver.close();
    }
}

execute_cypher(0)

Note: I do not encourage to use the above code as I do know its drawbacks. It's solely for example purpose. As @stdob-- has showed how to do it using promises I just gave an example of doing it in a different way which is used back when I was new for node.js and neo4j. And now with my experience I recommend to use the Neo4j: Cypher - UNWIND as its the best practice.

Upvotes: 1

stdob--
stdob--

Reputation: 29172

You can use the Promise.all function since returned values will be in order of the Promises passed:

var cyphers = [
    `MATCH (a) RETURN count(a) AS nodesCount`,
    `MATCH ()-[r]->() RETURN count(r) AS relsCount`
]

var session = driver.session()

var queries = []
cyphers.forEach(function(cypher) {
    queries.push(session.run(cypher))
})

Promise.all(queries).then(function(results) {
    results.forEach(function (result) {
        console.log(result)
    })
    session.close()
    driver.close()
})

Upvotes: 2

Related Questions