简体   繁体   English

使用nodejs的Neo4j查询

[英]Neo4j query with nodejs

I have an array of 1000 cypher (neo4j) queries (in string form). 我有一个1000个密码(neo4j)查询数组(以字符串形式)。

when I loop (for loop, individual values) through this array in session, all queries are going in sequence. 当我在会话中遍历此数组(for循环,各个值)时,所有查询都按顺序进行。 Though I receive 1000 results back also, however, the result of order is changed. 虽然我也收到了1000个结果,但是订单的结果却改变了。

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...] 例如a = [t1,t2,t3,t4 ...]

the result from cypher can be in any order say t2,t1,t4,t3 密码的结果可以是任意顺序,例如t2,t1,t4,t3

I want the result in same t1, t2, t3, t4 manner 我想要以相同的t1,t2,t3,t4方式显示结果

Any suggestion, please? 有什么建议吗?

You can use the Promise.all function since returned values will be in order of the Promises passed : 您可以使用Promise.all函数,因为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()
})

Are you trying to store the data and does all the cypher perform same operation with same attrib and props? 您是否要存储数据,并且所有密码都使用相同的attrib和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. 如果是,那么为什么不使用unwind功能,该工作将在一个密码中完成,而在一个电话中也将完成。 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. 即使有不同类型的密码,将它们分组并使用展开,这也会导致较少的会话(几乎1)以及连接,从而导致性能提高,与执行一个密码相比,产生的时间也更少一次。

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: 如果您不存储数据,则可以使用promise概念的node.js或创建如下的编码结构:

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. 正如@ stdob--展示了如何使用Promise进行操作,我仅举了一个以不同方式进行操作的示例,该示例在我刚接触Node.js和neo4j时就用过。 And now with my experience I recommend to use the Neo4j: Cypher - UNWIND as its the best practice . 现在,根据我的经验, 我建议使用Neo4j:Cypher-UNWIND作为最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM