Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DocumentArray<T>

Type parameters

  • T: MongooseDocument

Hierarchy

  • DocumentArray<T>
    • DocumentArray

Indexable

[n: number]: T

Index

Properties

length

length: number

Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.

Methods

$pop

  • $pop(): T
  • Pops the array atomically at most one time per document save(). Calling this mulitple times on an array before saving sends the same command as calling it once. This update is implemented using the MongoDB $pop method which enforces this restriction.

    Returns T

$shift

  • $shift(): T
  • Atomically shifts the array at most one time per document save(). Calling this mulitple times on an array before saving sends the same command as calling it once. This update is implemented using the MongoDB $pop method which enforces this restriction.

    Returns T

addToSet

  • addToSet(...args: any[]): T[]
  • Adds values to the array if not already present.

    Parameters

    • Rest ...args: any[]

    Returns T[]

    the values that were added

concat

  • concat(...items: ConcatArray<T>[]): T[]
  • concat(...items: (T | ConcatArray<T>)[]): T[]
  • Combines two or more arrays.

    Parameters

    • Rest ...items: ConcatArray<T>[]

      Additional items to add to the end of array1.

    Returns T[]

  • Combines two or more arrays.

    Parameters

    • Rest ...items: (T | ConcatArray<T>)[]

      Additional items to add to the end of array1.

    Returns T[]

create

  • create(obj: any): T
  • Creates a subdocument casted to this schema. This is the same subdocument constructor used for casting.

    Parameters

    • obj: any

      the value to cast to this arrays SubDocument schema

    Returns T

every

  • every(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean
  • Determines whether all the members of an array satisfy the specified test.

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => unknown

      A function that accepts up to three arguments. The every method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value false, or until the end of the array.

        • (value: T, index: number, array: T[]): unknown
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns unknown

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns boolean

filter

  • filter<S>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]
  • filter(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]
  • Returns the elements of an array that meet the condition specified in a callback function.

    Type parameters

    • S: T

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => value is S

      A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.

        • (value: T, index: number, array: T[]): value is S
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns value is S

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns S[]

  • Returns the elements of an array that meet the condition specified in a callback function.

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => unknown

      A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.

        • (value: T, index: number, array: T[]): unknown
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns unknown

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns T[]

forEach

  • forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void
  • Performs the specified action for each element in an array.

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => void

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

        • (value: T, index: number, array: T[]): void
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns void

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns void

id

  • id(id: ObjectId | string | number | NativeBuffer): T
  • Searches array items for the first document with a matching _id.

    Parameters

    • id: ObjectId | string | number | NativeBuffer

    Returns T

    the subdocument or null if not found.

indexOf

  • indexOf(obj: any): number
  • Return the index of obj or -1 if not found.

    Parameters

    • obj: any

      the item to look for

    Returns number

inspect

  • inspect(): T[]
  • Helper for console.log

    Returns T[]

join

  • join(separator?: string): string
  • Adds all the elements of an array separated by the specified separator string.

    Parameters

    • Optional separator: string

      A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

    Returns string

lastIndexOf

  • lastIndexOf(searchElement: T, fromIndex?: number): number
  • Returns the index of the last occurrence of a specified value in an array.

    Parameters

    • searchElement: T

      The value to locate in the array.

    • Optional fromIndex: number

      The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.

    Returns number

map

  • map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]
  • Calls a defined callback function on each element of an array, and returns an array that contains the results.

    Type parameters

    • U

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => U

      A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

        • (value: T, index: number, array: T[]): U
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns U

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns U[]

nonAtomicPush

  • nonAtomicPush(...args: any[]): number
  • Marks the entire array as modified, which if saved, will store it as a $set operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it.

    Parameters

    • Rest ...args: any[]

    Returns number

    new length of the array

pop

  • pop(): T
  • Wraps Array#pop with proper change tracking. marks the entire array as modified which will pass the entire thing to $set potentially overwritting any changes that happen between when you retrieved the object and when you save it.

    Returns T

pull

  • pull(...args: any[]): this
  • Pulls items from the array atomically. Equality is determined by casting the provided value to an embedded document and comparing using the Document.equals() function.

    Parameters

    • Rest ...args: any[]

    Returns this

push

  • push(...args: any[]): number
  • Wraps Array#push with proper change tracking.

    Parameters

    • Rest ...args: any[]

    Returns number

    new length of the array

reduce

  • reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T
  • reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T
  • reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U
  • Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Parameters

    • callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue: T, currentValue: T, currentIndex: number, array: T[]): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    Returns T

  • Parameters

    • callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T
        • (previousValue: T, currentValue: T, currentIndex: number, array: T[]): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    • initialValue: T

    Returns T

  • Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Type parameters

    • U

    Parameters

    • callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue: U, currentValue: T, currentIndex: number, array: T[]): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns U

    • initialValue: U

      If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

    Returns U

reduceRight

  • reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T
  • reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T
  • reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U
  • Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Parameters

    • callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue: T, currentValue: T, currentIndex: number, array: T[]): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    Returns T

  • Parameters

    • callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T
        • (previousValue: T, currentValue: T, currentIndex: number, array: T[]): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    • initialValue: T

    Returns T

  • Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Type parameters

    • U

    Parameters

    • callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue: U, currentValue: T, currentIndex: number, array: T[]): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns U

    • initialValue: U

      If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

    Returns U

remove

  • remove(...args: any[]): this
  • Alias of pull

    Parameters

    • Rest ...args: any[]

    Returns this

reverse

  • reverse(): T[]
  • Reverses the elements in an Array.

    Returns T[]

set

  • set(i: number, val: any): this
  • Sets the casted val at index i and marks the array modified.

    Parameters

    • i: number
    • val: any

    Returns this

shift

  • shift(): T
  • Wraps Array#shift with proper change tracking. Marks the entire array as modified, which if saved, will store it as a $set operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it.

    Returns T

slice

  • slice(start?: number, end?: number): T[]
  • Returns a section of an array.

    Parameters

    • Optional start: number

      The beginning of the specified portion of the array.

    • Optional end: number

      The end of the specified portion of the array. This is exclusive of the element at the index 'end'.

    Returns T[]

some

  • some(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean
  • Determines whether the specified callback function returns true for any element of an array.

    Parameters

    • callbackfn: (value: T, index: number, array: T[]) => unknown

      A function that accepts up to three arguments. The some method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value true, or until the end of the array.

        • (value: T, index: number, array: T[]): unknown
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns unknown

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns boolean

sort

  • sort(compareFn?: (a: T, b: T) => number): this
  • Sorts an array.

    Parameters

    • Optional compareFn: (a: T, b: T) => number

      Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.

      [11,2,22,1].sort((a, b) => a - b)
        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns this

splice

  • splice(...args: any[]): T[]
  • Wraps Array#splice with proper change tracking and casting. Marks the entire array as modified, which if saved, will store it as a $set operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it.

    Parameters

    • Rest ...args: any[]

    Returns T[]

toLocaleString

  • toLocaleString(): string
  • Returns a string representation of an array. The elements are converted to string using their toLocalString methods.

    Returns string

toObject

  • toObject(options?: any): T[]
  • Returns a native js Array of plain js objects

    Parameters

    • Optional options: any

      optional options to pass to each documents toObject method call during conversion

    Returns T[]

toString

  • toString(): string
  • Returns a string representation of an array.

    Returns string

unshift

  • unshift(...args: any[]): number
  • Wraps Array#unshift with proper change tracking. Marks the entire array as modified, which if saved, will store it as a $set operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it.

    Parameters

    • Rest ...args: any[]

    Returns number

Generated using TypeDoc