mongoloquent
    Preparing search index...

    Class QueryBuilder<T>

    QueryBuilder class for MongoDB operations with Mongoloquent Provides a fluent interface for building MongoDB queries with advanced features like soft deletes, timestamps, relationships, and more.

    Type Parameters

    • T

      The document type that this query will operate on

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    $alias: string = ""

    Alias for relationship

    $changes: Partial<Record<keyof T, any>> = {}

    Changes made to the document

    $collection: string = "mongoloquent"

    Collection name

    $connection: string = ""

    MongoDB connection string

    $databaseName: string = ""

    Database name

    $deletedAt: string = "deletedAt"

    Field name for the deletedAt timestamp

    $id: null | string | ObjectId = null

    Document ID

    $isDeleted: string = "isDeleted"

    Field name for the isDeleted flag

    $limit: number = 0

    Number of documents to limit in query

    $lookups: Document[] = []

    Lookup stages for aggregation pipelines

    $options: IRelationOptions = {}

    Relationship options

    $original: Partial<T> = {}

    Original document data

    $useSoftDelete: boolean = false

    Flag to enable soft delete functionality

    $useTimestamps: boolean = true

    Flag to enable timestamps

    $collection: string = ""

    Collection name

    $connection: string = MONGOLOQUENT_DATABASE_URI

    Default MongoDB connection string

    $databaseName: string = MONGOLOQUENT_DATABASE_NAME

    Default database name

    $isDeleted: string = "isDeleted"

    Field name for the isDeleted flag

    $schema: any

    Schema definition for the document

    $timezone: string = TIMEZONE

    Field name for the timezone

    $useSoftDelete: boolean = false

    Flag to enable soft delete functionality

    $useTimestamps: boolean = true

    Flag to enable timestamps

    Methods

    • Returns the average value of a field

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • field: K

        Field name

      Returns Promise<number>

      Average value

    • Alias for insert - creates a new document in the collection

      Parameters

      • doc: FormSchema<T>

        Document to create

      • Optionaloptions: InsertOneOptions

        MongoDB insert options

      Returns Promise<T>

      Created document with _id

    • Alias for insertMany - creates multiple documents in the collection

      Parameters

      • docs: FormSchema<T>[]

        Array of documents to create

      • Optionaloptions: BulkWriteOptions

        MongoDB bulk write options

      Returns Promise<ObjectId[]>

      Array of created document IDs

    • Deletes documents by IDs (soft delete if enabled)

      Parameters

      • ...ids: (string | ObjectId | (string | ObjectId)[])[]

        IDs of documents to delete

      Returns Promise<number>

      Number of documents deleted or soft-deleted

    • Returns the first document matching the query as an enhanced instance

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Optional fields to select

      Returns Promise<null | QueryBuilder<T> & T>

      First matching document or null if none found

    • Returns first matching document or creates a new one

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find existing document

      • Optionaldoc: Partial<FormSchema<T>>

        Data to use for creation if no match is found

      Returns Promise<T>

      Existing or newly created document

    • Returns the first document matching the query or throws exception if none found

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...columns: (K | K[])[]

        Optional fields to select

      Returns Promise<QueryBuilder<T> & T>

      First matching document

      If no document found

    • Alias for firstOrCreate - returns existing document or creates a new model instance

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find existing document

      • Optionaldoc: Partial<FormSchema<T>>

        Data to use for the new instance if no match is found

      Returns Promise<T>

      Existing or newly created document

    • Permanently deletes soft-deleted documents by IDs

      Parameters

      • ...ids: (string | ObjectId | (string | ObjectId)[])[]

        IDs of documents to permanently delete

      Returns Promise<number>

      Number of documents permanently deleted

      If force deletion fails

    • Executes the query and returns all matching documents

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Optional fields to select

      Returns Promise<Collection<T>>

      Collection of matching documents

      If query execution fails

    • Gets the MongoDB collection

      Parameters

      • Optionalcollection: string

        Optional collection name to override the default

      Returns Collection<FormSchema<T>>

      MongoDB collection object

    • Gets original values of specified fields

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to get original values for

      Returns any

      Original values

    • Inserts a document into the collection

      Parameters

      • doc: FormSchema<T>

        Document to insert

      • Optionaloptions: InsertOneOptions

        MongoDB insert options

      Returns Promise<T>

      Inserted document with _id

      If insertion fails

    • Inserts multiple documents into the collection

      Parameters

      • docs: FormSchema<T>[]

        Array of documents to insert

      • Optionaloptions: BulkWriteOptions

        MongoDB bulk write options

      Returns Promise<ObjectId[]>

      Array of inserted document IDs

      If bulk insertion fails

    • Checks if all of the specified fields are unchanged

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to check

      Returns boolean

      True if all fields are clean, false otherwise

    • Checks if any of the specified fields have been changed

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to check

      Returns boolean

      True if any field is dirty, false otherwise

    • Returns the maximum value of a field

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • field: K

        Field name

      Returns Promise<number>

      Maximum value

    • Returns the minimum value of a field

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • field: K

        Field name

      Returns Promise<number>

      Minimum value

    • Sets the order for the query results

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column to order by

      • Optionaldirection: "asc" | "desc" = "asc"

        Sort direction (asc or desc)

      • OptionalcaseSensitive: boolean = false

        Whether sorting should be case sensitive

      Returns this

      Current query builder instance

    • Adds an OR where condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • operator: any

        Operator or value if comparing equality

      • Optionalvalue: any = null

        Value to compare against (optional if operator is the value)

      Returns QueryBuilder<T>

      Current query builder instance

    • Adds an OR where between condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • values: [number, number?]

        Array with lower and upper bounds

      Returns QueryBuilder<T>

      Current query builder instance

    • Returns paginated results with metadata

      Parameters

      • Optionalpage: number = 1

        Page number (starting from 1)

      • Optionallimit: number = 15

        Number of items per page

      Returns Promise<IModelPaginate>

      Object containing data and pagination metadata

      If pagination fails

    • Returns the sum of values for a field

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • field: K

        Field name

      Returns Promise<number>

      Sum of values

    • Protected

      Tracks changes to a field

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • field: K

        Field being changed

      • value: any

        New value

      Returns void

    • Updates a document in the collection based on the current query

      Parameters

      • doc: Partial<FormSchema<T>>

        Document fields to update

      • Optionaloptions: FindOneAndUpdateOptions = {}

        MongoDB findOneAndUpdate options

      Returns Promise<null | WithId<FormSchema<T>>>

      Updated document

      If update fails

    • Updates multiple documents in the collection matching the current query

      Parameters

      • doc: Partial<FormSchema<T>>

        Document fields to update

      • Optionaloptions: UpdateOptions

        MongoDB updateMany options

      Returns Promise<number>

      Number of documents modified

      If bulk update fails

    • Updates a document if it exists, otherwise creates it

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find the document

      • Optionaldoc: Partial<FormSchema<T>>

        Document fields to update or insert

      Returns Promise<T | WithId<FormSchema<T>>>

      Updated or created document

    • Alias for updateOrCreate

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find the document

      • Optionaldoc: Partial<FormSchema<T>>

        Document fields to update or insert

      Returns Promise<T | WithId<FormSchema<T>>>

      Updated or created document

    • Checks if any of the specified fields were changed

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to check

      Returns boolean

      True if any field was changed, false otherwise

    • Adds a where condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • operator: any

        Operator or value if comparing equality

      • Optionalvalue: any = null

        Value to compare against (optional if operator is the value)

      Returns QueryBuilder<T>

      Current query builder instance

    • Adds a where between condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • values: [number, number?]

        Array with lower and upper bounds

      Returns QueryBuilder<T>

      Current query builder instance