# Find & Query

Source: https://docs.rushdb.com/2.x/learn/records-and-queries/find-and-query/

RushDB provides four read methods: look up by ID, find one, find unique, or run a full search query with filtering, aggregation, and relationship traversal — all through a unified `SearchQuery` object.

---

## Find by ID

**Python**

`db.records.find_by_id()`

```python
# Single record
movie = db.records.find_by_id("movie-123")

# Multiple records
movies = db.records.find_by_id(["movie-123", "movie-456"])
```

**TypeScript**

`db.records.findById()`

```typescript
const movie = await db.records.findById('movie-id-123')
const movies = await db.records.findById(['id-1', 'id-2', 'id-3'])
```

Returns `DBRecordInstance` (single) or `DBRecordsArrayInstance` (array).

**Shell**

`GET /api/v1/records/:entityId`

```bash
curl https://api.rushdb.com/api/v1/records/movie-123 \
  -H "Authorization: Bearer $RUSHDB_API_KEY"
```

---

## Find One

Returns the first matching record, or `null` / `None` if none found.

**Python**

`db.records.find_one()`

```python
movie = db.records.find_one({
    "labels": ["MOVIE"],
    "where": {"title": "Inception"}
})
```

**TypeScript**

`db.records.findOne()`

```typescript
const movie = await db.records.findOne({
  labels: ['MOVIE'],
  where: { title: 'Inception' }
})
```

**Shell**

Use `POST /api/v1/records/search` with `"limit": 1`.

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"labels": ["MOVIE"], "where": {"title": "Inception"}, "limit": 1}'
```

---

## Find Unique

Like `find_one` / `findOne`, but throws `NonUniqueResultError` if more than one record matches.

**Python**

`db.records.find_uniq()`

```python
movie = db.records.find_uniq({
    "labels": ["MOVIE"],
    "where": {"title": "Inception"}
})
```

**TypeScript**

`db.records.findUniq()`

```typescript

try {
  const movie = await db.records.findUniq({
    labels: ['MOVIE'],
    where: { title: 'Inception' }
  })
} catch (e) {
  if (e instanceof NonUniqueResultError) console.error(`found ${e.count} matches`)
}
```

**Shell**

Not a dedicated endpoint — check `total` from a search response to verify uniqueness.

---

## Search Records

Full search with filtering, sorting, and pagination.

**Python**

`db.records.find()`

```python
result = db.records.find({
    "labels": ["MOVIE"],
    "where": {"rating": {"$gte": 8}, "genre": "sci-fi"},
    "orderBy": {"rating": "desc"},
    "limit": 20,
    "skip": 0
})

for movie in result:
    print(movie.get("title"), movie.get("rating"))

print(f"{len(result)} shown, {result.total} total")
```

**TypeScript**

`db.records.find()`

```typescript
const { data: movies, total } = await db.records.find({
  labels: ['MOVIE'],
  where: { rating: { $gte: 8 }, genre: 'sci-fi' },
  orderBy: { rating: 'desc' },
  limit: 20,
  skip: 0
})
```

**Shell**

`POST /api/v1/records/search`

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["MOVIE"],
    "where": {"rating": {"$gte": 8}, "genre": "sci-fi"},
    "orderBy": {"rating": "desc"},
    "limit": 20,
    "skip": 0
  }'
```

### SearchQuery parameters

| Field     | Type               | Description                                                                                   |
| --------- | ------------------ | --------------------------------------------------------------------------------------------- |
| `labels`  | `string[]`         | Filter by one or more labels                                                                  |
| `where`   | `object`           | Field conditions and operators (see [Where Operators](/learn/search-query/where-operators))   |
| `orderBy` | `string \| object` | Sort criteria (see [Pagination & Order](/learn/search-query/pagination-order))                |
| `limit`   | `number`           | Max records to return (default: 1000). **Omit when using `select`**                           |
| `skip`    | `number`           | Records to skip for pagination                                                                |
| `select`  | `object`           | Output-shaping expressions (see [Select Expressions](/learn/search-query/select-expressions)) |
| `groupBy` | `string[]`         | Grouping keys, e.g. `['$record.genre']`                                                       |

---

## Where Operators

```python title="Python"
# Numeric comparisons
{"rating": {"$gt": 7, "$lt": 10}}
{"rating": {"$gte": 8, "$lte": 9.5}}

# Set membership
{"genre": {"$in": ["sci-fi", "thriller"]}}
{"genre": {"$nin": ["romance"]}}

# Text matching
{"title": {"$contains": "dark"}}
{"title": {"$startsWith": "The"}}
{"title": {"$endsWith": "Returns"}}

# Existence and type checks
{"poster": {"$exists": True}}
{"rating": {"$type": "number"}}

# Logical operators
{"$and": [{"genre": "sci-fi"}, {"rating": {"$gte": 8}}]}
{"$or":  [{"genre": "sci-fi"}, {"rating": {"$gte": 9}}]}
{"$not": {"genre": "romance"}}
```

Full reference: [Where Operators](/learn/search-query/where-operators)

---

## Relationship Traversal

Filter across graph edges inline with `where`:

**Python**

```python
# Movies that have an ACTOR from the USA
result = db.records.find({
    "labels": ["MOVIE"],
    "where": {
        "ACTOR": {"country": "USA"}
    }
})

# With explicit relationship type and direction
result = db.records.find({
    "labels": ["MOVIE"],
    "where": {
        "ACTOR": {
            "$relation": {"type": "STARS_IN", "direction": "in"},
            "country": "USA"
        }
    }
})
```

**TypeScript**

```typescript
// Movies that have an ACTOR from the USA
const { data } = await db.records.find({
  labels: ['MOVIE'],
  where: {
    ACTOR: { country: 'USA' }
  }
})

// With explicit relationship type and direction
const { data: films } = await db.records.find({
  labels: ['MOVIE'],
  where: {
    DIRECTOR: {
      $relation: { type: 'DIRECTED_BY', direction: 'out' },
      name: { $contains: 'Nolan' }
    }
  }
})
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["MOVIE"],
    "where": {
      "ACTOR": {
        "$relation": {"type": "STARS_IN", "direction": "in"},
        "country": "USA"
      }
    }
  }'
```

### Multi-hop and cycles

Add `hops` to `$relation` to traverse a variable number of hops (`type`/`direction` apply to every hop; criteria filter only the final record), and use the `$cycle` operator — its value is the traversal spec itself, with `hops` mandatory (`min` ≥ 2) — to find records on a closed path back to themselves:

**Python**

```python
# Employees up to 4 hops down Alice's reporting chain
result = db.records.find({
    "labels": ["EMPLOYEE"],
    "where": {
        "EMPLOYEE": {
            "$relation": {"type": "REPORTS_TO", "direction": "in", "hops": {"min": 1, "max": 4}},
            "name": "Alice"
        }
    }
})

# Accounts on a directed transfer ring of 2-6 hops
result = db.records.find({
    "labels": ["ACCOUNT"],
    "where": {
        "$cycle": {"type": "TRANSFERRED_TO", "direction": "out", "hops": {"min": 2, "max": 6}}
    }
})
```

**TypeScript**

```typescript
// Employees up to 4 hops down Alice's reporting chain
const { data } = await db.records.find({
  labels: ['EMPLOYEE'],
  where: {
    EMPLOYEE: {
      $relation: { type: 'REPORTS_TO', direction: 'in', hops: { min: 1, max: 4 } },
      name: 'Alice'
    }
  }
})

// Accounts on a directed transfer ring of 2-6 hops
const { data: rings } = await db.records.find({
  labels: ['ACCOUNT'],
  where: {
    $cycle: { type: 'TRANSFERRED_TO', direction: 'out', hops: { min: 2, max: 6 } }
  }
})
```

Full semantics — hop caps, untyped traversal, aggregation caveats: [Where Operators](/learn/search-query/where-operators#variable-length-traversal-hops)

---

## Select Expressions & Aggregations

:::danger Do not set `limit` when using `select`
It restricts the record scan and produces mathematically incorrect totals. Use `orderBy` on a `select` output key instead.
:::

**Python**

```python
result = db.records.find({
    "labels": ["MOVIE"],
    "where": {"ACTOR": {"$alias": "$actor", "country": "USA"}},
    "select": {
        "title":      "$record.title",
        "actorCount": {"$count": "$actor"},
        "avgRating":  {"$avg": "$record.rating", "$precision": 1},
        "actorNames": {"$collect": {
            "from": "$actor",
            "select": {"name": "$actor.name"}
        }}
    }
})
```

Available expressions: `$count` · `$sum` · `$avg` · `$min` · `$max` · `$collect` · `$timeBucket` · `$ref` · `$add` · `$subtract` · `$multiply` · `$divide`

**TypeScript**

```typescript
const stats = await db.records.find({
  labels: ['MOVIE'],
  where: { ACTOR: { $alias: '$actor', country: 'USA' } },
  select: {
    title: '$record.title',
    actorCount: { $count: '$actor' },
    avgRating: { $avg: '$record.rating', $precision: 1 },
    actorNames: { $collect: { from: '$actor', select: { name: '$actor.name' } } }
  }
})
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["MOVIE"],
    "select": {
      "count":     {"$count": "*"},
      "avgRating": {"$avg": "$record.rating"}
    }
  }'
```

Full reference: [Select Expressions](/learn/search-query/select-expressions)

---

## Group By

**Python**

```python
result = db.records.find({
    "labels": ["MOVIE"],
    "select": {
        "count":     {"$count": "*"},
        "avgRating": {"$avg": "$record.rating"}
    },
    "groupBy": ["$record.genre"],
    "orderBy": {"count": "desc"}
})
# [{"genre": "sci-fi", "count": 42, "avgRating": 7.9}, ...]
```

**TypeScript**

```typescript
const byGenre = await db.records.find({
  labels: ['MOVIE'],
  select: {
    count: { $count: '*' },
    avgRating: { $avg: '$record.rating', $precision: 1 }
  },
  groupBy: ['$record.genre'],
  orderBy: { count: 'desc' }
})
// [{ genre: 'sci-fi', count: 42, avgRating: 7.9 }, ...]
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["MOVIE"],
    "select": {
      "count":     {"$count": "*"},
      "avgRating": {"$avg": "$record.rating"}
    },
    "groupBy": ["$record.genre"],
    "orderBy": {"count": "desc"}
  }'
```

Full reference: [Group By](/learn/search-query/group-by)

---

## TimeBucket (Time-Series)

**Python**

```python
result = db.records.find({
    "labels": ["ORDER"],
    "select": {
        "day":   {"$timeBucket": {"field": "$record.createdAt", "unit": "day"}},
        "count": {"$count": "*"}
    },
    "groupBy": ["day"],
    "orderBy": {"day": "asc"}
})
```

**TypeScript**

```typescript
const daily = await db.records.find({
  labels: ['ORDER'],
  select: {
    day: { $timeBucket: { field: '$record.createdAt', unit: 'day' } },
    count: { $count: '*' }
  },
  groupBy: ['day'],
  orderBy: { day: 'asc' }
})
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["ORDER"],
    "select": {
      "day":   {"$timeBucket": {"field": "$record.createdAt", "unit": "day"}},
      "count": {"$count": "*"}
    },
    "groupBy": ["day"],
    "orderBy": {"day": "asc"}
  }'
```

`unit` values: `day` · `week` · `month` · `quarter` · `year` · `hours` · `minutes` · `seconds` (use plural + `size` for custom window widths).

---

## Collect Related Records

Pull related records inline with the query result.

**Label-based** (preferred for nesting — no alias required):

**Python**

```python
result = db.records.find({
    "labels": ["COMPANY"],
    "select": {
        "name": "$record.name",
        "departments": {
            "$collect": {
                "label": "DEPARTMENT",
                "select": {
                    "name": "$self.name",
                    "projects": {
                        "$collect": {
                            "label": "PROJECT",
                            "select": {"name": "$self.name"}
                        }
                    }
                }
            }
        }
    }
})
```

**TypeScript**

```typescript
const companies = await db.records.find({
  labels: ['COMPANY'],
  select: {
    name: '$record.name',
    departments: {
      $collect: {
        label: 'DEPARTMENT',
        select: {
          name: '$self.name',
          projects: {
            $collect: {
              label: 'PROJECT',
              select: { name: '$self.name' }
            }
          }
        }
      }
    }
  }
})
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["COMPANY"],
    "select": {
      "name": "$record.name",
      "departments": {
        "$collect": {
          "label": "DEPARTMENT",
          "select": {"name": "$self.name"}
        }
      }
    }
  }'
```

**Alias-based** (requires `$alias` in `where`):

**Python**

```python
result = db.records.find({
    "labels": ["MOVIE"],
    "where": {"ACTOR": {"$alias": "$actor"}},
    "select": {
        "title":  "$record.title",
        "actors": {
            "$collect": {
                "from": "$actor",
                "select": {"name": "$actor.name", "country": "$actor.country"}
            }
        }
    }
})
```

**TypeScript**

```typescript
const movies = await db.records.find({
  labels: ['MOVIE'],
  where: { ACTOR: { $alias: '$actor' } },
  select: {
    title: '$record.title',
    actors: {
      $collect: {
        from: '$actor',
        select: { name: '$actor.name', country: '$actor.country' }
      }
    }
  }
})
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "labels": ["MOVIE"],
    "where": {"ACTOR": {"$alias": "$actor"}},
    "select": {
      "title":  "$record.title",
      "actors": {
        "$collect": {
          "from": "$actor",
          "select": {"name": "$actor.name", "country": "$actor.country"}
        }
      }
    }
  }'
```

---

## SearchResult

**Python**

```python
result = db.records.find({"labels": ["MOVIE"], "limit": 10})

len(result)       # records returned in this page (≤ limit)
result.total      # total records matching in the database
result.has_more   # True if more pages remain
result[0]         # access by index
for r in result:  # iterable
    pass
```

**TypeScript**

```typescript
const { data, total } = await db.records.find({ labels: ['MOVIE'], limit: 10 })
// data: DBRecordInstance[]
// total: number — total records matching in the database
```

**Shell**

```json
{
  "data": [...],
  "total": 42
}
```

---

## Contextual Search (REST)

Search within a specific record's relationships:

```bash
POST /api/v1/records/:entityId/search
```

```bash
curl -X POST https://api.rushdb.com/api/v1/records/movie-123/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"labels": ["ACTOR"], "where": {"country": "USA"}}'
```

---

## In a Transaction

**Python**

```python
tx = db.tx.begin()
try:
    result = db.records.find({"labels": ["MOVIE"]}, transaction=tx)
    tx.commit()
except Exception:
    tx.rollback()
    raise
```

**TypeScript**

```typescript
const tx = await db.tx.begin()
try {
  const { data } = await db.records.find({ labels: ['MOVIE'] }, tx)
  await tx.commit()
} catch (e) {
  await tx.rollback()
  throw e
}
```

**Shell**

```bash
curl -X POST https://api.rushdb.com/api/v1/records/search \
  -H "Authorization: Bearer $RUSHDB_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Transaction-Id: $TX_ID" \
  -d '{"labels": ["MOVIE"]}'
```

---

## TypeScript: Via Model

```typescript
const MovieModel = new Model('MOVIE', {
  title: { type: 'string' },
  rating: { type: 'number' }
})

const all = await MovieModel.find()
const sciFi = await MovieModel.find({ where: { genre: 'sci-fi' } })
const one = await MovieModel.findOne({ where: { title: 'Inception' } })
const byId = await MovieModel.findById('movie-id-123')
const unique = await MovieModel.findUniq({ where: { title: 'Inception' } })
```

Model search methods auto-fill `labels` from the model definition.

---

## See also

- [Where Operators](/learn/search-query/where-operators) — full operator reference
- [Select Expressions](/learn/search-query/select-expressions) — aggregation & output shaping
- [Group By](/learn/search-query/group-by) — grouping reference
- [Pagination & Order](/learn/search-query/pagination-order) — sorting and paging
- [Connect Records](/learn/relationships/connect-records) — traverse and filter relationships
- [Semantic Search](/learn/semantic-search) — vector + filter search
