Class TableFindCursor<T, TRaw>

A subclass of FindCursor which is identical to its parent; it just adds some more specific typing for a couple of properties/functions.

See FindCursor directly for information on the cursor itself.

Type Parameters

Hierarchy (view full)

Constructors

Properties

#buffer: TRaw[] = []
#consumed: number = 0
#filter: [Filter, boolean]
#mapping?: ((doc) => T)

Type declaration

    • (doc): T
    • Parameters

      • doc: any

      Returns T

#nextPageState?: null | string
#serdes: SerDes<any, any, any>
#sortVector?: null | DataAPIVector
#state: FindCursorStatus = ...

Accessors

Methods

  • An async iterator that lazily iterates over all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was broken early.

    Returns AsyncGenerator<T, void, void>

    Example

    for await (const doc of cursor) {
      console.log(doc);
    }
  • Consumes up to max records from the buffer, or all records if max is not provided.

    Note that this actually consumes the buffer; it doesn't just peek at it.

    Parameters

    • Optional max: number

      The maximum number of records to read from the buffer. If not provided, all records will be read.

    Returns TRaw[]

    The records read from the buffer.

  • Returns the number of records that have been read be the user from the cursor.

    Unless the cursor was closed before the buffer was completely read, the total number of records retrieved from the server is equal to (FindCursor.consumed + FindCursor.buffered).

    Returns number

    The number of records that have been read be the user from the cursor.

  • Sets the filter for the cursor, overwriting any previous filter.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new filter set.

    Parameters

    Returns FindCursor<T, TRaw>

    A new cursor with the new filter set.

  • Iterates over all records in the cursor, calling the provided consumer for each record.

    If the consumer returns false, iteration will stop.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was stopped early.

    Parameters

    • consumer: ((doc) => boolean) | ((doc) => void)

      The consumer to call for each record.

    Returns Promise<void>

    A promise that resolves when iteration is complete.

  • Retrieves the vector used to perform the vector search, if applicable.

    • If includeSortVector is not true, this will unconditionally return null. No find request will be made.

    • If sort: { $vector } was used, getSortVector() will simply regurgitate that same $vector.

    • If sort: { $vectorize } was used, getSortVector() will return the $vector that was created from the text.

    • If vector search is not used, getSortVector() will simply return null. A find request will still be made.

    If includeSortVector is true, and this function is called before any other cursor operation (such as .next() or .toArray()), it'll make an API request to fetch the sort vector, filling the cursor's buffer in the process.

    If the cursor has already been executed before this function has been called, no additional API request will be made to fetch the sort vector, as it has already been cached.

    But to reiterate, if includeSortVector is false, and this function is called, no API request is made, and the cursor's buffer is not populated; it simply returns null.

    Returns Promise<null | DataAPIVector>

    The sort vector, or null if none was used (or if includeSortVector !== true).

  • Tests if there is a next record in the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return false.

    Returns Promise<boolean>

    Whether or not there is a next record.

  • Sets whether similarity scores should be included in the cursor's results.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new similarity setting.

    Parameters

    • Optional includeSimilarity: boolean

      Whether similarity scores should be included.

    Returns FindCursor<WithSim<TRaw>, WithSim<TRaw>>

    A new cursor with the new similarity setting.

  • Sets whether the sort vector should be fetched on the very first API call. Note that this is a requirement to use FindCursor.getSortVector—it'll unconditionally return null if this is not set to true.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new setting.

    Parameters

    • includeSortVector: boolean = true

      Whether the sort vector should be fetched on the first API call

    Returns FindCursor<T, TRaw>

    A new cursor with the new sort vector inclusion setting.

  • Sets the maximum number of records to return.

    If limit == 0, there will be no limit on the number of records returned.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new limit set.

    Parameters

    • limit: number

      The limit for this cursor.

    Returns FindCursor<T, TRaw>

    A new cursor with the new limit set.

  • Map all records using the provided mapping function. Previous mapping functions will be composed with the new mapping function (new ∘ old).

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new mapping set.

    You may NOT set a projection after a mapping is already provided, to prevent potential de-sync errors. If you really want to do so, you may use FindCursor.clone to create a new cursor with the same configuration, but without the mapping, and then set the projection.

    Type Parameters

    • R

    Parameters

    • mapping: ((doc) => R)

      The mapping function to apply to all records.

        • (doc): R
        • Parameters

          • doc: T

          Returns R

    Returns FindCursor<R, TRaw>

    A new cursor with the new mapping set.

  • Fetches the next record from the cursor. Returns null if there are no more records to fetch.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return null.

    Returns Promise<null | T>

    The next record, or null if there are no more records.

  • Sets the projection for the cursor, overwriting any previous projection.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new projection set.

    To properly type this method, you should provide a type argument to specify the shape of the projected records.

    Note that you may NOT provide a projection after a mapping is already provided, to prevent potential de-sync errors. If you really want to do so, you may use FindCursor.clone to create a new cursor with the same configuration, but without the mapping, and then set the projection.

    Type Parameters

    Parameters

    • projection: Projection

      Specifies which fields should be included/excluded in the returned records.

    Returns FindCursor<RRaw, RRaw>

    A new cursor with the new projection set.

    Example

    const cursor = table.find({ name: 'John' });

    // T is `any` because the type is not specified
    const rawProjected = cursor.project({ id: 0, name: 1 });

    // T is { name: string }
    const projected = cursor.project<{ name: string }>({ id: 0, name: 1 });

    // You can also chain instead of using intermediate variables
    const fluentlyProjected = table
      .find({ name: 'John' })
      .project<{ name: string }>({ id: 0, name: 1 });

    // It's important to keep mapping in mind
    const mapProjected = table
      .find({ name: 'John' })
      .map(doc => doc.name);
      .project<string>({ id: 0, name: 1 });
  • Rewinds the cursor to its uninitialized state, clearing the buffer and any state. Any configuration set on the cursor will remain, but iteration will start from the beginning, sending new queries to the server, even if the resultant data was already fetched by this cursor.

    Returns void

  • Sets the number of records to skip before returning.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new skip set.

    Parameters

    • skip: number

      The skip for the cursor query.

    Returns FindCursor<T, TRaw>

    A new cursor with the new skip set.

  • Sets the sort criteria for prioritizing records.

    NB. This method does NOT mutate the cursor, and may be called even after the cursor is started; it simply returns a new, uninitialized cursor with the given new sort set.

    Parameters

    • sort: Sort

      The sort order to prioritize which records are returned.

    Returns FindCursor<T, TRaw>

    A new cursor with the new sort set.

  • Returns an array of all matching records in the cursor. The user should ensure that there is enough memory to store all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return an empty array.

    Returns Promise<T[]>

    An array of all records in the cursor.