Aggregates
The second argument to .query()
allows you to specify aggregates on the query.
Using the keyword aggregation
along with the below options, the result object will return with aggregate data.
Currently the only available aggregation operation is count
.
{
aggregation: [
{ofIndex: "title", aggregate: "count"}
]
}
ofIndex
: The index to apply the aggregation to.
aggregate
: The aggregation operation.
For more information on what CipherStash can and can’t do currently go here
import { Stash } from "@cipherstash/stashjs"
import { generateSchemaWithMapping } from "./schemaWithMapping";
const matchQuery = async () => {
try {
const stash = await Stash.connect();
const movieSchema = await generateSchemaWithMapping()
const movies = await stash.loadCollection(movieSchema)
const queryResult = await movies.query(
movie => movie.title.match("ody"),
{
aggregation: [
{ofIndex: "title", aggregate: "count"}
]
}
)
console.log(queryResult)
return queryResult
} catch (err) {
console.error(err)
console.error(`Could not query collection! Reason: ${JSON.stringify(err)}`)
return
}
}
matchQuery()
The aggregate details are returned as part of the results object in the aggregates field.
aggregates: [ { name: 'count', value: 2n } ]
{
took: 0.101377625,
documents: [
{
titleType: 'video',
primaryTitle: 'A Bloody Show: John Wesley Harding & Friends Live at Bumbershoot 2005',
originalTitle: 'A Bloody Show: John Wesley Harding & Friends Live at Bumbershoot 2005',
startYear: 2006,
runtimeMinutes: 89,
genres: 'Music',
year: 2006,
title: 'A Bloody Show: John Wesley Harding & Friends Live at Bumbershoot 2005',
runningTime: 89
},
{
titleType: 'movie',
primaryTitle: 'The Odyssey',
originalTitle: 'Al-Oudyssa',
startYear: 2004,
runtimeMinutes: 92,
genres: 'Crime,Drama,Thriller',
year: 2004,
title: 'The Odyssey',
runningTime: 92
}
],
aggregates: [ { name: 'count', value: 2n } ]
}
Skip results
skipResults
can be passed as another keyword argument, if you only want the aggregate data returned and not the actual records.
This can speed up the query and avoid sending back unnecessary data.
const queryResult = await movies.query(
movie => movie.title.match("ody"),
{
aggregation: [
{ofIndex: "title", aggregate: "count"}
],
skipResults: true
}
)