# QUERY 運算式

本頁說明 `QUERY` 如何依欄位篩選 JSON 文件、其運算式文法，以及如何在 Go 中建立相同的 filter。

## QUERY

```text
QUERY active = true
QUERY profile.level >= 3
QUERY role = admin AND active = true LIMIT 20
QUERY (role = admin OR role = editor) AND NOT disabled = true
```

只有具已預熱解析快取的 JSON 項目會被評估。條件讀取點記法欄位，以 `utils.Vtoa` 呈現（`1.0` 變為 `1`，`true` 維持 `true`），再以運算子比較。欄位不存在時條件為 false，`NE` 亦同。

每行結果為 `key: <raw JSON>`，而非只有鍵。

## 運算式文法

```text
expression := or
or         := and ( OR and )*
and        := not ( AND not )*
not        := NOT not | primary
primary    := "(" expression ")" | field operator value
```

- `AND`、`OR`、`NOT` 不分大小寫；`AND` 優先於 `OR`。
- 輸入先依空白切分，再從每個 token 剝除開頭 `(` 與結尾 `)`，因此 `(role` 與 `editor)` 可用。
- 值是單一 token，不能含空白。
- 殘留 token 會失敗：`unexpected word: ...`；缺少運算子、值或 `)` 也會失敗。

## 程式化篩選

```go
f := filter.And{
    filter.GTE{Field: "profile.level", Value: "3"},
    filter.Not{filter.EQ{Field: "disabled", Value: "true"}},
}
rows := torii.Query(f, 20)

parsed, err := filter.AtoFilter("role = admin OR role = editor")
```

| 型別 | 種類 |
|---|---|
| `And`、`Or` | `[]Filter` |
| `Not` | `[1]Filter` |
| `Cond{Field, Op, Value}` | 通用條件 |
| `EQ`、`NE`、`GT`、`GTE`、`GE`、`LT`、`LTE`、`LE`、`LIKE` | `{Field, Value}` 簡寫 |

## 相關頁面

- [運算子與 FIND](/zh/querying-operators)
- [JSON 文件](/zh/json-documents)
- [filter](/zh/api-reference-filter)
