---
title: "YAML"
description: "如何定义、编写和查询 YAML 数据。"
canonical_url: "https://nuxt-content.zhcndoc.com/docs/files/yaml"
---
# YAML

> 如何定义、编写和查询 YAML 数据。

## 定义集合

```ts [content.config.ts]
import { defineCollection, defineContentConfig } from '@nuxt/content'
import { z } from 'zod'

export default defineContentConfig({
  collections: {
    authors: defineCollection({
      type: 'data',
      source: 'authors/**.yml',
      schema: z.object({
        name: z.string(),
        avatar: z.string(),
        url: z.string()
      })
    })
  }
})
```

## 创建 `.yml` 文件

在 `content/authors/` 目录下创建作者文件。

<code-group>

```yaml [farnabaz.yml]
name: Ahad Birang
avatar: https://avatars.githubusercontent.com/u/2047945?v=4
url: https://github.com/farnabaz
```

```yaml [larbish.yml]
name: Baptiste Leproux
avatar: https://avatars.githubusercontent.com/u/7290030?v=4
url: https://github.com/larbish
```

</code-group>

## 查询数据

现在我们可以查询作者数据了：

```vue
<script lang="ts" setup>
// 查找单个作者
const { data: author } = await useAsyncData('larbish', () => {
  return queryCollection('authors')
    .where('stem', '=', 'authors/larbish')
    .first()
})

// 获取所有作者
const { data: authors } = await useAsyncData('authors', () => {
  return queryCollection('authors')
    .order('name', 'DESC')
    .all()
})
</script>
```


## Sitemap

See the full [sitemap](https://nuxt-content.zhcndoc.com/sitemap.md) for all pages.
