Skip to main content

MongoDB Query Builder

The query builder provides a fluent API for building complex MongoDB queries. Access it via mongo.from(Collection).query().

Basic Usage

const users = await mongo
.from(User)
.query()
.where("email", "test@example.com")
.many();

Filtering

const users = await mongo.from(User).query().where("age", "$gte", 18).many();

Sorting

const users = await mongo.from(User).query().sort({ name: -1 }).many();

Limiting and Offsetting

const users = await mongo.from(User).query().limit(10).offset(5).many();

Combining Filters

const users = await mongo
.from(User)
.query()
.where("age", "$gte", 18)
.sort({ name: 1 })
.limit(10)
.many();

whereIn / whereNotIn

const users = await mongo
.from(User)
.query()
.whereIn("name", ["Alice", "Bob"])
.many();

whereNull / whereNotNull

const users = await mongo.from(User).query().whereNull("email").many();

Raw Queries

const users = await mongo
.from(User)
.query()
.rawWhere({ email: { $exists: false } })
.many();

Best Practices

  • Use .limit() and .sort() for pagination.
  • Use .rawWhere() for advanced MongoDB queries.

Next: MongoDB Sessions & Transactions