> ## Documentation Index
> Fetch the complete documentation index at: https://docs.therefrain.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# メモリ操作

> メモリコレクションを使ってステップ間でデータを蓄積・集計します。

## 概要

**メモリ操作**により、複数のステップやループの反復にわたってデータを収集・集計できます。データは名前付き**コレクション**に保存され、実行全体を通じて永続化します。

## 2つの操作タイプ

| タイプ         | 説明             | 用途                |
| ----------- | -------------- | ----------------- |
| `append`    | コレクションにアイテムを追加 | 行、レコード、キャプチャ値の蓄積  |
| `aggregate` | コレクションのサマリーを計算 | 合計、カウント、最小/最大値の算出 |

## append

キャプチャした値を名前付きコレクションに追加します：

```yaml theme={null}
steps:
  - ordinal: 0
    description: "商品データを抽出"
    action:
      type: extract
      script: "document.querySelector('.product-name').textContent"
    url: "https://app.example.com/products"
    riskLevel: low
    requiresConfirmation: false
    captures:
      - name: productName
        strategy: snapshot
        description: "商品名"
    memoryOperations:
      - type: append
        collection: products
        source: productName
```

| フィールド        | 型          |  必須 | 説明           |
| ------------ | ---------- | :-: | ------------ |
| `type`       | `"append"` | Yes | 操作タイプ        |
| `collection` | string     | Yes | 対象コレクション名    |
| `source`     | string     |  No | 追加するキャプチャ変数名 |

## aggregate

コレクションからサマリー値を計算します：

```yaml theme={null}
steps:
  - ordinal: 5
    description: "合計を計算"
    action:
      type: memory
    url: "https://app.example.com/products"
    riskLevel: low
    requiresConfirmation: false
    memoryOperations:
      - type: aggregate
        collection: prices
        operation: sum
        field: price
        outputVariable: totalPrice
```

| フィールド            | 型             |  必須 | 説明         |
| ---------------- | ------------- | :-: | ---------- |
| `type`           | `"aggregate"` | Yes | 操作タイプ      |
| `collection`     | string        | Yes | ソースコレクション名 |
| `operation`      | AggregateOp   | Yes | 集約操作       |
| `field`          | string        |  No | 集約対象フィールド  |
| `outputVariable` | string        |  No | 結果を格納する変数  |

## 集約操作

| 操作             | 説明      | 結果例         |
| -------------- | ------- | ----------- |
| `sum`          | 数値の合計   | `1500`      |
| `count`        | アイテム数   | `25`        |
| `avg`          | 数値の平均   | `60.5`      |
| `min`          | 最小値     | `10`        |
| `max`          | 最大値     | `200`       |
| `concat`       | 文字列値の連結 | `"A, B, C"` |
| `unique_count` | ユニーク値の数 | `18`        |

## 完全な例

商品リストを複数ページにわたってスクレイピングし、CSV にエクスポートします：

```yaml theme={null}
title: 商品カタログのスクレイピング
steps:
  - ordinal: 0
    description: "商品リストに遷移"
    action:
      type: navigate
      url: "https://store.example.com/products"
    url: "https://store.example.com/products"
    riskLevel: low
    requiresConfirmation: false

  - ordinal: 1
    description: "現在のページの商品をループ処理"
    action:
      type: extract
      script: "JSON.stringify([...document.querySelectorAll('.product')].map(e => e.textContent))"
    url: "https://store.example.com/products"
    riskLevel: low
    requiresConfirmation: false
    loop:
      forEach: "collection:productElements"
      itemVariable: product
      maxIterations: 100
    steps:
      - ordinal: 0
        description: "商品名をキャプチャ"
        action:
          type: extract
          script: "document.querySelector('.product-name').textContent"
        url: "https://store.example.com/products"
        riskLevel: low
        requiresConfirmation: false
        captures:
          - name: name
            strategy: snapshot
        memoryOperations:
          - type: append
            collection: products
            source: name

  - ordinal: 2
    description: "商品数をカウント"
    action:
      type: memory
    url: "https://store.example.com/products"
    riskLevel: low
    requiresConfirmation: false
    memoryOperations:
      - type: aggregate
        collection: products
        operation: count
        outputVariable: productCount

  - ordinal: 3
    description: "商品を CSV にエクスポート"
    action:
      type: export
      exportCollection: products
      exportFormat: csv
      exportPath: "./output/products.csv"
    url: "https://store.example.com/products"
    riskLevel: low
    requiresConfirmation: false
```

## コレクションの反復処理

`forEach` ループで `collection:name` 構文を使用してコレクション内のアイテムを反復処理できます：

```yaml theme={null}
loop:
  forEach: "collection:products"
  itemVariable: product
```

ループの詳細は[ループ・分岐](/ja/yaml/loops-and-branches)を参照してください。
