ToollessToolless

Query Operators

Filter documents with query operators

Comparison Operators

$eq

Matches values equal to a specified value:

eq.ts
await users.find({ status: { $eq: "active" } }).toArray();

// Shorthand (implicit $eq)
await users.find({ status: "active" }).toArray();

$ne

Matches values not equal to a specified value:

ne.ts
await users.find({ status: { $ne: "inactive" } }).toArray();

$gt / $gte

Greater than / Greater than or equal:

gt.ts
await users.find({ age: { $gt: 18 } }).toArray();
await users.find({ age: { $gte: 21 } }).toArray();

$lt / $lte

Less than / Less than or equal:

lt.ts
await users.find({ age: { $lt: 65 } }).toArray();
await users.find({ price: { $lte: 100 } }).toArray();

$in

Matches any value in an array:

in.ts
await users.find({ role: { $in: ["admin", "moderator"] } }).toArray();

$nin

Matches none of the values in an array:

nin.ts
await users.find({ status: { $nin: ["banned", "suspended"] } }).toArray();

Logical Operators

$and

Joins query clauses with logical AND:

and.ts
await users
  .find({
    $and: [{ age: { $gte: 18 } }, { status: "active" }],
  })
  .toArray();

// Implicit $and (shorthand)
await users
  .find({
    age: { $gte: 18 },
    status: "active",
  })
  .toArray();

$or

Joins query clauses with logical OR:

or.ts
await users
  .find({
    $or: [{ role: "admin" }, { permissions: { $in: ["write"] } }],
  })
  .toArray();

$nor

Joins query clauses with logical NOR (none match):

nor.ts
await users
  .find({
    $nor: [{ status: "banned" }, { status: "suspended" }],
  })
  .toArray();

$not

Inverts the effect of a query expression:

not.ts
await users
  .find({
    age: { $not: { $lt: 18 } },
  })
  .toArray();

Element Operators

$exists

Matches documents that have the specified field:

exists.ts
// Find documents with email field
await users.find({ email: { $exists: true } }).toArray();

// Find documents without email field
await users.find({ email: { $exists: false } }).toArray();

$type

Matches documents where field is of specified type:

type.ts
await users.find({ age: { $type: "number" } }).toArray();

Supported types: "string", "number", "boolean", "object", "array", "date", "regex", "null"

String Operators

$regex

Matches strings using regular expressions:

regex.ts
// Case-insensitive search
await users
  .find({
    name: { $regex: /alice/i },
  })
  .toArray();

// String pattern
await users
  .find({
    email: { $regex: "@gmail.com$" },
  })
  .toArray();

Array Operators

$elemMatch

Matches documents where an array element matches all specified conditions:

elemMatch.ts
await orders
  .find({
    items: {
      $elemMatch: {
        product: "laptop",
        quantity: { $gte: 2 },
      },
    },
  })
  .toArray();

$all

Matches arrays containing all specified elements:

all.ts
await posts
  .find({
    tags: { $all: ["javascript", "tutorial"] },
  })
  .toArray();

$size

Matches arrays with the specified length:

size.ts
await users
  .find({
    tags: { $size: 3 },
  })
  .toArray();

Combining Operators

Operators can be combined for complex queries:

combined.ts
const results = await users
  .find({
    $and: [
      { age: { $gte: 18, $lte: 65 } },
      {
        $or: [{ role: "admin" }, { verified: true }],
      },
      { tags: { $in: ["premium", "vip"] } },
      { email: { $regex: /@company\.com$/i } },
    ],
  })
  .toArray();

Nested Field Queries

Query nested objects using dot notation:

nested.ts
await users
  .find({
    "address.city": "New York",
    "address.zip": { $regex: /^10/ },
  })
  .toArray();
Dot notation works for any depth of nesting: 'a.b.c.d'

On this page