用数组查询cassandra数据库

Querying cassandra database with an array

我正在尝试查询我的 cassandra 数据库以从数组服务器端保存的名称列表中获取 return 数据。这是作为一个数组保存的。 我知道我正在访问的数据作为字符串存储在我的数据库中,因此我在它周围附加了单引号(我尝试过使用和不使用它但没有运气)。

这是我的查询。

const arr = ["ukcust1","ukcust2","ukcust5"];
//Here I append single quotes before and after to each string if needed

const query = "SELECT * FROM table_name WHERE name = ?";
client.execute(query, arr, { prepare:true }, function (err, result) {
..//Code
};

我在这里错过了什么?我希望查询是:

SELECT * FROM table_name WHERE name = each of the names in the array 'arr';

如果名称是集群键,那么您可以使用 "in" 和 "allow filtering" 进行查询,如下所示:

select * from table_name where name in ('ukcust1','ukcust2','ukcust3') allow filtering

假设名称不是聚类键,您可以使用聚类键(例如,date_of_birth),如果它在逻辑上有意义——也就是说,如果按日期过滤与名称相关—— - 像这样:

select * from table_name where date_of_birth in (1969, 1972) name in ('ukcust1','ukcust2','ukcust3') allow filtering

如果您做不到这些,您将需要使用 Javascript(例如 foreach)遍历数组。

查询参数的正确输入是数值数组。在这种情况下,它将是包含单个项目的参数数组,即名称数组。

const arr = ["ukcust1","ukcust2","ukcust5"];
const query = "SELECT * FROM table_name WHERE name = ?";

// Note the array containing a single item
const parameters = [ arr ];

client.execute(query, parameters, { prepare: true }, callback);

在文档中查看更多信息:https://docs.datastax.com/en/developer/nodejs-driver/3.5/faq/#how-can-i-use-a-list-of-values-with-the-in-operator-in-a-where-clause