简体   繁体   中英

Query Parameter Format for SELECT … IN with Cassandra using Node.js Driver

I have a Cassandra SELECT query with an IN parameter that I want to run via the Node driver, but can't figure out the syntax.

On the cqlsh console, I can run this select and get a correct result:

SELECT * FROM sourcedata WHERE company_id = 4 AND item_id in (ac943b6f-0143-0e1f-5282-2d39209f3a7a,bff421a0-c465-0434-8806-f128612b6850,877ddb6d-a164-1152-da77-1ec4c4468258);

However, trying to run this query using an array of IDs using the Cassandra Node driver, I get various errors depending on the format. Here's what I've tried:

client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (?)", [id, item_ids], function(err, rs) { ...

The error is:

ResponseError: Invalid list literal for item_id of type uuid

With this:

client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (?)", [id, item_ids], function(err, rs) { ...

The error is:

ResponseError: line 1:72 no viable alternative at input '[' (...WHERE company_id = 4 AND [item_id] in...)

item_ids is an array of string objects, and they were acquired via a select on another Cassandra table.

This is a working app, and other queries that don't use "SELECT .. IN" work fine.

I can also do make it work the "ugly" way, but would prefer not to:

client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (" + item_ids.toString() + ")", [id,], function(err, rs) { ...

You should use IN ? without parenthesis, to provide a list:

const query = 'SELECT * FROM sourcedata WHERE company_id = ? AND item_id in ?';
client.execute(query, [ id, item_ids ], { prepare: true }, callback);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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