Skip to content

Latest commit

 

History

History
227 lines (163 loc) · 5.61 KB

1.query-content.md

File metadata and controls

227 lines (163 loc) · 5.61 KB

queryContent()

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/ directory
const contentQuery = queryContent()

// Create a query looking into content/articles directory
const contentQuery = queryContent('articles')

// Create a query looking into content/articles/nuxt3 directory
const contentQuery = queryContent('articles', 'nuxt3')

where(query)

  • query{lang=ts}:
    • Type: Partial<QueryBuilderParams>{lang=ts}
    • Required

Filter results by query.

Where queries are based on subset of Mongo query syntax, it handles: $eq{lang=ts}, $ne{lang=ts}, $gt{lang=ts}, $gte{lang=ts}, $lt{lang=ts}, $lte{lang=ts} and $in{lang=ts}

// Implicit (assumes $eq operator)
const articles = await queryContent('articles').where({ title: 'Home' }).findOne()

// Explicit $eq
const articles = await queryContent('articles').where({ title: { $eq: 'Home' } }).findOne()

// $gt
const articles = await queryContent('articles').where({ age: { $gt: 18 } }).find()

// $in
const articles = await queryContent('articles').where({ name: { $in: ['odin', 'thor'] } }).find()

In order to filter in objects and an array or arrays, you can use the 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{lang="ts"}
    • Type: object{lang="ts"}
    • Required

Sort results by a field or fields.

// Sort by title ascending
const articles = await queryContent('articles')
  .sort({ title: 1 })
  .find()

// Sort by title ascending first then sort by category descending
const articles = await queryContent('articles')
  .sort({ title: 1, category: -1 })
  .find()
// OR
const articles = await queryContent('articles')
  .sort({ title: 1 })
  .sort({ category: -1 })
  .find()

// Sort by nested field
const articles = await queryContent('articles')
  .sort({ 'category.title': 1 })
  .find()

sort(){lang="ts"} method does case-sensitive, alphabetical sort by default. There is some magical options you can pass to sort options to change sort behavior, like sorting case-insensitive or numerically rather than alphabetically.

  • $sensitivity{lang=ts}: Change case sensitivity. Like using $sensitivity: 'base'{lang=ts} for case-insensitive sort
  • $numeric{lang=ts}: Boolean whether numeric collation should be used, such that "1" < "2" < "10".
  • $caseFirst{lang=ts}: Whether upper case or lower case should sort first.

For example, to sort a list of people from youngest to oldest:

const people = await queryContent('people')
  .sort({ age: 1, $numeric: true })
  .find()

These options are given to Intl.Collator().

limit(count)

  • count{lang="ts"}
    • Type: number{lang="ts"}
    • Required

Limit number of results.

// fetch only 5 articles
const articles = await queryContent('articles').limit(5).find()

skip(count)

  • count{lang=ts}
    • Type: number{lang=ts}
    • Required

Skip results.

// fetch the next 5 articles
const articles = await queryContent('articles')
    .skip(5)
    .limit(5)
    .find()

without(keys)

  • keys{lang=ts}
    • Type: string[]{lang=ts} or string{lang=ts}
    • Required

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{lang=ts}
    • Type: string[]{lang=ts} or string{lang=ts}
    • Required

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 articles
const articles = await queryContent('articles').find()

findOne()

Fetch the first match of content.

const firstArticle = await queryContent('articles').findOne()

findSurround(path, options)

  • path{lang=ts}
    • Type: string{lang=ts}
    • Required
  • options{lang=ts}
    • Type: { before: number, after: number }{lang=ts}
    • Default: { before: 1, after: 1 }{lang=ts}

Get the previous and next results around the path. The path should be the full path of the target content.

You will always obtain an array of fixed length filled with the matching document or null.

const [prev, next] = await queryContent()
  .only(['_path', 'title'])
  .sort({ date: 1})
  .where({ isArchived: false })
  .findSurround('/articles/article-2')

// Returns
[
  {
    title: 'Article 1',
    _path: '/articles/article-1'
    //...
  },
  null // no article-3 here
]

count()

Count the number of matched contents based on the query.

::alert This feature is available in edge-channel and will be released in next version. ::

// Count of articles
const count = await queryContent('articles').count()

// Returns
5 // number of articles

You can also use count() with where() method.

// Count of articles
const count = await queryContent('articles')
  .where({ isArchived: false })
  .count()

// Returns
5 // number of articles