gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:41:50 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com