javascript - 参数未知时的 Cypher MATCH 请求

标签 javascript node.js neo4j ecmascript-6 cypher

我正在尝试使用 javascript 实现一种 OGM。它不会完全是 OGM,但它适用于我的项目。 但是我需要能够使用像

这样的方法
User.find({ where : {name:}})

有点像 Sequelize 对 SQL 的作用。 但是,如果我尝试这样做:

run('MATCH (n:User {where}) RETURN ID(n) as id', { where: where });

Cypher 回答了这个问题:

Parameter maps cannot be used in MATCH patterns 
(use a literal map instead, eg. "{id: {param}.id}")

另一个帖子解释说原因是:

"Unlike properties in CREATE, MATCH requires the map to be a literal. This is because the property names must be known in advance, when the query is compiled, in order to efficiently plan its execution."

有人知道当我事先没有属性名称时如何给请求设置参数吗? (我的 GrapQL API 可以接收很多参数)...

非常感谢!

最佳答案

有些像这样:

WITH {where} as params
MATCH (n:User) WHERE ALL(k in keys(params) WHERE params[k] = n[k])
RETURN id(n)

或者,正如@logisma 所说——在脚本端生成一个请求:

var params = [];
Object.keys(where).forEach( function(key) {
    var str = "`" + key + "`: " + JSON.stringify( where[key] );
    params.push( str );
});
run('MATCH (n:User {' + params.join(", ") + '}) RETURN ID(n) as id')

关于javascript - 参数未知时的 Cypher MATCH 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353902/

相关文章:

javascript - Office 加载项开发 : Insert image/picture in Word 2016

node.js - 如何在谷歌云构建上升级 Node js 版本

neo4j - 在 Cypher 查询中结束 UNWIND 语句

java - neo4j 中的 GraphDatabaseService 和 NeoService 有什么区别

javascript - Notepad++ 只在替换中粘贴复制代码的第一行?

javascript - 如何在 angularJs 1.3 中设置 cookie 过期时间?

node.js - 没有强错误处理程序的环回错误

java - 比较 : neo4j vs titan

Javascript 正向回顾替代方案

javascript - 我可以使 node.js FTP 同步吗?