Skip to content

Logical Operators

Logical operators allow you to combine multiple conditions in a single query. UQL uses a MongoDB-inspired syntax that is 100% valid JSON.

NameDescription
$andjoins query clauses with a logical AND (default).
$orjoins query clauses with a logical OR, returns records that match any clause.
$notnegates the given clause.
$norjoins query clauses with a logical OR and then negates the result.

 

The $and operator is implicit when you specify multiple fields in the $where object.

You write
import { User } from './shared/models/index.js';
// Implicit AND
const users = await querier.findMany(User, {
$where: { name: 'roger', status: 'active' },
});
Generated SQL (PostgreSQL)
SELECT * FROM "User" WHERE "name" = $1 AND "status" = $2

The same query with an explicit $and:

You write
const users = await querier.findMany(User, {
$where: {
$and: [{ name: 'roger' }, { status: 'active' }]
},
});
Generated SQL (PostgreSQL)
-- Same result as implicit AND
SELECT * FROM "User" WHERE "name" = $1 AND "status" = $2

Logical operators can be nested to create complex filters.

You write
const users = await querier.findMany(User, {
$where: {
$or: [
{ name: { $istartsWith: 'A' } },
{
$and: [
{ status: 'pending' },
{ createdAt: { $lt: new Date('2025-01-01') } }
]
}
]
},
});
Generated SQL (PostgreSQL)
SELECT * FROM "User"
WHERE "name" ILIKE $1
OR ("status" = $2 AND "createdAt" < $3)

$not wraps conditions with NOT. It can be used at the field level or at the top level as an array of clauses.

Field-level $not
const users = await querier.findMany(User, {
$where: {
status: 'active',
name: { $not: { $startsWith: 'test' } },
},
});
Generated SQL (PostgreSQL)
SELECT * FROM "User"
WHERE "status" = $1 AND NOT ("name" LIKE $2)
Top-level $not
const users = await querier.findMany(User, {
$where: {
$not: [{ name: 'admin' }, { status: 'banned' }],
},
});
Generated SQL (PostgreSQL)
SELECT * FROM "User"
WHERE NOT ("name" = $1 AND "status" = $2)

$nor negates combined OR conditions — records match only if none of the clauses are true.

You write
const users = await querier.findMany(User, {
$where: {
$nor: [{ name: 'admin' }, { status: 'banned' }],
},
});
Generated SQL (PostgreSQL)
SELECT * FROM "User"
WHERE NOT ("name" = $1 OR "status" = $2)