Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<Document>

Collection of documents which are kept indexed.

Type parameters

Hierarchy

  • Collection

Implements

  • Iterable<Document>

Constructors

constructor

Accessors

count

  • get count(): number
  • Get the number of documents in the Collection.

    Returns number

    Total number of documents.

indexes

  • Returns the indexed properties.

    Returns IndexedKey<Document>[]

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Cursor<Document>
  • Iterate over Collection documents.

    Returns Cursor<Document>

clear

  • clear(): void

createIndexIfMissing

  • createIndexIfMissing(key: IndexedKey<Document>): boolean
  • Create index for property only if is not indexed already.

    Parameters

    Returns boolean

    Whether index was created or not. Returning false means index was present already.

createIndexes

  • createIndexes(...keys: IndexedKey<Document>[]): void
  • Create indexes for document keys.

    Parameters

    • Rest ...keys: IndexedKey<Document>[]

      Keys to be indexed.

    Returns void

delete

drop

  • drop(): void

dropIndexes

  • dropIndexes(...keys: IndexedKey<Document>[]): void
  • Remove indexes associated with keys.

    Parameters

    • Rest ...keys: IndexedKey<Document>[]

      Name of properties, associated indexes of which needs to be removed.

    Returns void

find

  • find(query?: Query<Document>, options?: Partial<FindOptions<Document>>): Document[]

insert

  • insert(documents: Document | Document[]): void
  • Inserts documents into Collection.
    Documents are validated according to JSON Schema (if given).
    When documents originality is DocumentOriginality.CLONE, they will be cloned, prior indexing.
    After indexing, DocumentNotification which contains inserted documents is emitted.

    Parameters

    • documents: Document | Document[]

    Returns void

map

  • map<MappedDocument>(mapper: Mapper<Document, MappedDocument>, options?: Partial<IndexOptions<Document>>): MappedDocument[]
  • Maps Collection documents.
    If you need to map only a subset of documents, you can specify an index that needs to be mapped by using options argument.

    Type parameters

    • MappedDocument

    Parameters

    • mapper: Mapper<Document, MappedDocument>

      Mapping function.

    • Optional options: Partial<IndexOptions<Document>>

      Options which control set of documents that needs to be mapped.

    Returns MappedDocument[]

    Mapped documents.

replace

  • replace(query: Query<Document>, replacement: Document, options?: Partial<ReplaceOptions<Document>>): Document[]

update

  • update(query: Query<Document>, update: ObjMap, options?: Partial<UpdateOptions<Document>>): Document[]
  • Modifies an existing document or documents. The method can modify specific fields of an existing document or documents.
    When one of the indexed properties is updated, documents will be re-indexed with the new values of them.
    Emits DocumentNotification with operation DocumentOperation.UPDATED which contains the updated documents.

    example

    Update document by id

    const update = {
      $set: {
          birthYear: 2000
      }
    };
    // returns old document
    collection.update('unique-id', update);
    

    Update multiple documents

    const update = {
      $inc: {
          salary: 100
      }
    };
    const query = {
      commits: {
          $gt: 25
      }
    };
    const options = {
      returnUpdates: true
    };
    // returns updated documents
    collection.update(query, update, options);
    

    Reindex renamed property

    const update = {
       $rename: {
           oldIndexName: newName
       }
    };
    // will de-index renamed index property
    collection.update('unique-id', update);
    
    const bringBackIndex = {
      $unset: {
          newName: ''
      },
      $set: {
          oldIndexName: 'new-value'
      }
    };
    collection.update('unique-id', bringBackIndex);
    

    Parameters

    • query: Query<Document>
    • update: ObjMap
    • Optional options: Partial<UpdateOptions<Document>>

    Returns Document[]

watch