The queryContent composable provides methods for querying and fetching your contents.
Create a query builder to search contents.
// Create a query looking for anything in content/ directoryconst contentQuery = queryContent()// Create a query looking into content/articles directoryconst contentQuery = queryContent('articles')// Create a query looking into content/articles/nuxt3 directoryconst contentQuery = queryContent('articles', 'nuxt3')
where(query)
query
:Partial<QueryBuilderParams>
Filter results by query.
Where queries are based on subset of Mongo query syntax, it handles: $eq
, $ne
, $gt
, $gte
, $lt
, $lte
and $in
// Implicit (assumes $eq operator)const articles = await queryContent('articles').where({ title: 'Home' }).findOne()// Explicit $eqconst articles = await queryContent('articles').where({ title: { $eq: 'Home' } }).findOne()// $gtconst articles = await queryContent('articles').where({ age: { $gt: 18 } }).find()// $inconst articles = await queryContent('articles').where({ name: { $in: ['odin', 'thor'] } }).find()
In order to filter in objects and array you can use nested properties style:
const products = await queryContent('products').where({ 'categories': { $contains: 'top' } }).find()const products = await queryContent('products').where({ 'categories': { $contains: ['top', 'woman'] } }).find()
sort(options)
options
object
Sort results by a field or fields.
// Sort by title ascendingconst articles = await queryContent('articles') .sort({ title: 1 }) .find()// Sort by title ascending first then sort by category descendingconst articles = await queryContent('articles') .sort({ title: 1, category: 0 }) .find()// ORconst articles = await queryContent('articles') .sort({ title: 1 }) .sort({ category: 0 }) .find()// Sort by nested fieldconst articles = await queryContent('articles') .sort({ 'category.title': 1 }) .find()
sort()
method does case-sensitive sort by default. There is some magical options you can pass to sort options to change sort behavior, like sorting case-insensitive.
$sensitivity
: Change case sensitivity. Like using$sensitivity: 'base'
for case-insensitive sort$numeric
: Whether numeric collation should be used, such that"1" < "2" < "10"
.$caseFirst
: Whether upper case or lower case should sort first.
These options are given to Intl.Collator().
limit(count)
count
Number
Limit number of results.
// fetch only 5 articlesconst articles = await queryContent('articles').limit(5).find()
skip(count)
count
Number
Skip results.
// fetch the next 5 articlesconst articles = await queryContent('articles') .skip(5) .limit(5) .find()
without(keys)
keys
Array
or String
Remove a subset of fields.
const articles = await queryContent('articles').without('unused-key').find()const articles = await queryContent('articles').without(['unused-key', 'another-unused-key']).find()
only(keys)
keys
Array
or String
Select a subset of fields.
const articles = await queryContent('articles').only('id').find()const articles = await queryContent('articles').only(['id', 'title']).find()
find()
Fetch and return the list of matched contents based on the query.
// List of articlesconst articles = await queryContent('articles').find()
findOne()
Fetch first matched content.
const firstArticle = await queryContent('articles').findOne()
findSurround(path, options)
path
String
options
{ before: number, after: number }
{ before: 1, after: 1 }
Get previous and next results around a specific path.
You will always obtain an array of fixed length filled with the matching document or null.
const [prev, next] = await queryContent('articles') .only(['_path', 'title']) .sort({ date: 1}) .where({ isArchived: false }) .findSurround('article-2')// Returns[ { title: 'Article 1', path: 'article-1' }, null // no article-3 here]