> For the complete documentation index, see [llms.txt](https://docs.formsort.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.formsort.com/conditions-and-logic/advanced-logic.md).

# Advanced logic

Enabling **Advanced logic** allows you to write custom boolean expressions using [MongoDB’s query and projection operators](https://www.mongodb.com/docs/manual/reference/operator/query/), giving you full flexibility over conditional rendering in your flow.

Instead of using a visual logic builder, you write logic directly in JSON format. Each answer variable maps to a field, and standard MongoDB query operators evaluate those fields.

#### Why JSON, not Drag-and-Drop?

While we may support a visual builder in the future, the JSON-based syntax is:

* Powerful and expressive
* Lightweight and easy to copy/paste
* Flexible enough for most complex logic needs

### Common patterns

#### **Both conditions must be true** — `$and`

Enable an item only if **both** conditions are satisfied:

* `distance_to_office` is less than or equal to 75
* `is_qualified` is true

```javascript
{
  "$and": [
    {
      "distance_to_office": {
        "$lte": 75
      }
    },
    {
      "is_qualified": true
    }
  ]
}
```

Read more about [$and](https://docs.mongodb.com/manual/reference/operator/query/and/).

#### **At least one condition is true -** $or

Enable an item if **either** of the following is true:

* `is_parent` is true
* `has_parent_consent` is true

```javascript
{
  "$or": [
    {
      "is_parent": true
    },
    {
      "has_parent_consent": true
    }
  ]
}
```

Read more about [$or](https://docs.mongodb.com/manual/reference/operator/query/or/).

#### **Answer is one of several values** — `$in`

Check if an answer matches **any** value from a list.

Enable if `country` is **CA**, **MX**, or **US**:

```javascript
{
  "country": {
    "$in": [
      "CA",
      "MX",
      "US"
    ]
  }
}
```

Read more about [$in](https://docs.mongodb.com/manual/reference/operator/query/in/).

#### **Answer is&#x20;*****not*****&#x20;in a list of values — `$nin`**

Check that an answer **does not** match any value from a list.

Enable if `state` is **not** NY or PA:

```javascript
{
  "state": {
    "$nin": [
      "NY",
      "PA"
    ]
  }
}
```

Read more about [$nin](https://docs.mongodb.com/manual/reference/operator/query/nin/#mongodb-query-op.-nin).

#### **Answer is defined** — `$exists`

Enable if a variable has **any value** (even `false`):

```javascript
{
  "income": {
    "$exists": true
  }
}
```

This mirrors the behavior of the **Is defined** simple logic operator.

Read more about [$exists](https://docs.mongodb.com/manual/reference/operator/query/exists/).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.formsort.com/conditions-and-logic/advanced-logic.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
