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.

import { pool } from './shared/orm.js';
import { User } from './shared/models/index.js';
// Implicit AND
const users = await pool.transaction(async (querier) => {
return querier.findMany(User, {
$where: { name: 'roger', status: 'active' },
});
});

The same query with an explicit $and:

const users = await querier.findMany(User, {
$where: {
$and: [{ name: 'roger' }, { status: 'active' }]
},
});

 

Logical operators can be nested to create complex filters.

const users = await querier.findMany(User, {
$where: {
$or: [
{ name: { $startsWith: 'A' } },
{
$and: [
{ status: 'pending' },
{ createdAt: { $lt: new Date('2025-01-01') } }
]
}
]
},
});

That ▲ code will generate clean, parameterized SQL:

SELECT * FROM "User"
WHERE "name" LIKE 'A%'
OR ("status" = 'pending' AND "createdAt" < '2025-01-01')