/** * Boolean Expression Operators * * Boolean expressions evaluate their argument expressions as booleans * and return a boolean as the result. * * In addition to the false boolean value, Boolean expression evaluates * as false the following: null, 0, and undefined values. * The Boolean expression evaluates all other values as true, including * non-zero numeric values and arrays. */ import { ComparisonQuery } from './comparison'; /** The $and operator performs a logical AND operation on an array of one or more expressions (e.g. , , etc.) and selects the documents that satisfy all the expressions in the array. The $and operator uses short-circuit evaluation. If the first expression (e.g. ) evaluates to false, MongoDB will not evaluate the remaining expressions. * Syntax: { $and: [ { }, { } , ... , { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and */ export const $and = "$and"; /** The $nor operator performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array. The $nor has the following syntax: * { $nor: [ { }, { }, ... { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/nor/#op._S_nor */ export const $nor = "$nor"; /** The $not operator performs a logical NOT operation on the specified and selects the documents that do not match the . This includes documents that do not contain the field. * Syntax: { field: { $not: { } } } * @see https://docs.mongodb.com/manual/reference/operator/query/not/#op._S_not */ export const $not = "$not"; /** The $or operator performs a logical OR operation on an array of two or more and selects the documents that satisfy at least one of the . The $or has the following syntax: * { $or: [ { }, { }, ... , { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/or/#op._S_or */ export const $or = "$or"; const LOGICAL_OPERATORS = [ $and, $not, $or, ] as const; export type LogicalQueryOperator = typeof LOGICAL_OPERATORS[number]; export const isBooleanExpressionOperator = ( val: any ): val is LogicalQueryOperator => val in LOGICAL_OPERATORS; type BooleanLike = ComparisonQuery; /** The $and operator performs a logical AND operation on an array of one or more expressions (e.g. , , etc.) and selects the documents that satisfy all the expressions in the array. The $and operator uses short-circuit evaluation. If the first expression (e.g. ) evaluates to false, MongoDB will not evaluate the remaining expressions. * Syntax: { $and: [ { }, { } , ... , { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and */ export type $andQuery = | { [$and]: [BooleanLike, ...BooleanLike[]]; } | [typeof $and, BooleanLike, ...BooleanLike[]]; /** The $nor operator performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array. The $nor has the following syntax: * { $nor: [ { }, { }, ... { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/nor/#op._S_nor */ export type $norQuery = { [$nor]: | [BooleanLike, ...BooleanLike[]]; } | [typeof $nor, BooleanLike, ...BooleanLike[]]; /** The $not operator performs a logical NOT operation on the specified and selects the documents that do not match the . This includes documents that do not contain the field. * Syntax: { field: { $not: { } } } * @see https://docs.mongodb.com/manual/reference/operator/query/not/#op._S_not */ export type $notQuery = | { [$not]: BooleanLike; } | [typeof $not, BooleanLike]; /** The $or operator performs a logical OR operation on an array of two or more and selects the documents that satisfy at least one of the . The $or has the following syntax: * { $or: [ { }, { }, ... , { } ] } * @see https://docs.mongodb.com/manual/reference/operator/query/or/#op._S_or */ export type $orQuery = | { [$or]: Record } | { [$or]: [BooleanLike, ...BooleanLike[]]; } | [typeof $or, BooleanLike, ...BooleanLike[]]; export type LogicalQuery = | $andQuery | $norQuery | $notQuery | $orQuery