Class MongoCollection<T>

A typed Mongo collection.

Type Parameters

  • T extends Document = Document

Hierarchy

  • MongoCollection

Constructors

Properties

collection: Collection<any>

The underlying collection.

The database.

Methods

  • Does a count on a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // count all documents
    const count1 = await my_collection.count()
    // count with filter
    const count2 = await my_collection.count({ foo: 'bar' })

    Parameters

    • Optional filter: Filter<T>

      The filter.

    • Optional options: CountDocumentsOptions

      Custom options.

    Returns Promise<number>

    The promise with the number of documents.

  • Create an index on that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    await my_collection.createIndex({
    foo: 1,
    bar: -1
    })

    Parameters

    • indexSpec: IndexSpecification

      The field or spec.

    • Optional options: CreateIndexesOptions

      Custom options.

    Returns Promise<string>

    The promise with the result.

  • Delete documents from that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // delete all documents with foo === 1
    await my_collection.deleteMany({
    foo: 1,
    })

    Parameters

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

      Custom options.

    Returns Promise<DeleteResult>

    The promise with the result.

  • Delete a document from that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // delete first document with foo === 1
    await my_collection.deleteOne({
    foo: 1,
    })

    Parameters

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

      Custom options.

    Returns Promise<DeleteResult>

    The promise with the result.

  • Does a find on that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // all, where foo === 1
    const docs: any[] = await my_collection.find({
    foo: 1,
    })

    Parameters

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<any>

      Custom options.

    Returns Promise<WithId<T>[]>

    The promise with the result.

  • Does a findOne on that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // (null) if not found
    const doc: any = await my_collection.findOne({
    foo: 1,
    })

    Parameters

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<any>

      Custom options.

    Returns Promise<null | WithId<T>>

    The promise with the result or (null) if not found.

  • Insert many documents into that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // insert an array of two documents
    await my_collection.insertMany([{
    foo: 1,
    }, {
    foo: 2,
    }])

    Parameters

    • docs: T[]

      The documents to insert.

    • Optional options: BulkWriteOptions

      Custom options.

    Returns Promise<InsertManyResult<T>>

    The promise with the result.

  • Insert one document into that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // insert a single document
    await my_collection.insertOne({
    foo: 1,
    })

    Parameters

    • doc: T

      The document to insert.

    • Optional options: BulkWriteOptions

      Custom options.

    Returns Promise<InsertOneResult<T>>

    The promise with the result.

  • Update documents in that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // update all with foo === 1
    // and set foo = 2 and remove bar prop
    await my_collection.updateMany({
    foo: 1
    }, {
    '$set': {
    foo : 2
    },
    '$unset': {
    bar: 1
    }
    })

    Parameters

    • filter: Filter<T>

      The filter for the documents.

    • update: UpdateFilter<T>

      The update query for the documents.

    • Optional options: UpdateOptions

      Custom options.

    Returns Promise<Document | UpdateResult<Document>>

    The promise with the result.

  • Update one document in that collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const my_collection = mongo.collection('my_collection')

    // update first matching one with foo === 1
    // and set foo = 2 and remove bar prop
    await my_collection.updateOne({
    foo: 1
    }, {
    '$set': {
    foo : 2
    },
    '$unset': {
    bar: 1
    }
    })

    Parameters

    • filter: Filter<T>

      The filter for the document.

    • update: UpdateFilter<T>

      The update query for the document.

    • Optional options: UpdateOptions

      Custom options.

    Returns Promise<Document | UpdateResult<Document>>

    The promise with the result.

Generated using TypeDoc