Total row count for Oracle NoSQL node driver

352 Views Asked by At

I'm running a very simple query in Oracle NoSQL (NodeJs driver):

let count_statement = `SELECT count(*) FROM ${resource}`;
let res = await this.client.query(count_statement, {});

This returns 0 rows (and thus no count). If I run the query without the count, I get back rows I can iterate over. Is there no way to get the total results for a query.

I don't want to count the results WITHIN the row. I need the number of rows that match this query (which is all of them in this query)

2

There are 2 best solutions below

0
John Connelly On

In order to get the full result set, the code should call client.query in a loop intil the continuationKey in the result object is null, as in this example from the nosql node sdk:

/*
 * Execute a query and print the results.  Optional limit parameter is used to
 * limit the results of each query API invocation to that many rows.
 */
async function runQuery(client, stmt, limit) {
    const opt = { limit };
    let res;
    console.log('Query results:');
    do {
        // Issue the query
        res = await client.query(stmt, opt);

        // Each call to NoSQLClient.query returns a portion of the
        // result set. Iterate over the result set, using the
        // QueryResult.continuationKey in the query's option parameter,
        // until the result set is exhausted and continuation key is
        // null.
        for(let row of res.rows) {
            console.log('  %O', row);
        }

        opt.continuationKey = res.continuationKey;
    } while(res.continuationKey != null);
}
0
Dario On

You can also use the new queryIterable syntax introduced in version 5.3.0

const count_statement = `SELECT count(*) FROM ${resource}`
const rows = []
for await(const res of client.queryIterable(count_statement )) {
    rows.push.apply(rows, res.rows);
}
return rows