Skip to content

QueryResult

Returned by all query methods (execute, executeCypher, etc.) and Transaction.execute.

Properties

Property Type Description
columns string[] Column names from the query
length number Number of rows in the result
executionTimeMs number \| null Query execution time in milliseconds
rowsScanned number \| null Number of rows scanned during execution

Methods

get()

Get a single row by index as a plain object with column names as keys.

get(index: number): object
const row = result.get(0);
console.log(row['p.name']);  // 'Alix'

Throws if index is out of range.

toArray()

Get all rows as an array of objects.

toArray(): object[]
const rows = result.toArray();
for (const row of rows) {
  console.log(row['p.name'], row['p.age']);
}

rows()

Get all rows as an array of arrays (no column names).

rows(): any[][]
const raw = result.rows();
// [[value1, value2], [value3, value4], ...]

scalar()

Get the first column of the first row as a single value.

scalar(): any
const count = (await db.execute('MATCH (n) RETURN count(n)')).scalar();
console.log(count);  // 42

Throws if the result has no rows or no columns.

nodes()

Get all nodes found in the result.

nodes(): JsNode[]
const result = await db.execute('MATCH (p:Person) RETURN p');
for (const node of result.nodes()) {
  console.log(node.id, node.labels);
}

edges()

Get all edges found in the result.

edges(): JsEdge[]

Example

const result = await db.execute('MATCH (p:Person) RETURN p.name, p.age');

// Column names
console.log(result.columns);  // ['p.name', 'p.age']

// Row count
console.log(result.length);  // 3

// Single row
const first = result.get(0);
console.log(first['p.name']);

// All rows as objects
for (const row of result.toArray()) {
  console.log(`${row['p.name']} is ${row['p.age']} years old`);
}

// Scalar value
const count = (await db.execute('MATCH (n) RETURN count(n)')).scalar();

// Metrics
console.log(`Query took ${result.executionTimeMs}ms`);
console.log(`Scanned ${result.rowsScanned} rows`);