Class MongoDatabase

A connection to a MongoDB database.

Hierarchy

  • MongoDatabase

Constructors

Properties

_client: Nilable<MongoClient>

The current connect.

The function, that returns the options for that instance.

Accessors

  • get isConnected(): boolean
  • Gets if instance is connected or not.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    // (true)
    mongo.isConnected

    await mongo.disconnect()

    // (false)
    mongo.isConnected

    Returns boolean

    Is connected or not.

Methods

  • Does a count on a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • 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 a collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Parameters

    • collectionName: string

      The collection's name.

    • indexSpec: IndexSpecification

      The field or spec.

    • Optional options: CreateIndexesOptions

      Custom options.

    Returns Promise<string>

    The promise with the result.

  • Delete documents from a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

      Custom options.

    Returns Promise<DeleteResult>

    The promise with the result.

  • Delete a document from a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

      Custom options.

    Returns Promise<DeleteResult>

    The promise with the result.

  • disconnect from database

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    await mongo.disconnect()

    Returns Promise<void>

  • Registers the process events to close the connect on exit.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    // return Exit Code = 3 to
    // operating system
    // if connection closes
    // or process is terminated
    mongo.exitOnClose(3)

    Parameters

    • exitCode: number = 2

      The custom exit code.

    Returns MongoDatabase

    This instance.

  • Does a find on a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<T>

      Custom options.

    Returns Promise<WithId<T>[]>

    The promise with the result.

  • Does a findOne on a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<T>

      Custom options.

    Returns Promise<null | WithId<T>>

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

  • Insert many documents into a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • docs: T[]

      The documents to insert.

    • Optional options: BulkWriteOptions

      Custom options.

    Returns Promise<InsertManyResult<T>>

    The promise with the result.

  • Insert one document into a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • doc: T

      The document to insert.

    • Optional options: BulkWriteOptions

      Custom options.

    Returns Promise<InsertOneResult<T>>

    The promise with the result.

  • Update documents in a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • 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 a MongoDB collection.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

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

    Type Parameters

    • T extends Document = Document

    Parameters

    • collectionName: string

      The collection's name.

    • 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.

  • Opens a new client connection if it doesn't exist and executes an action on it.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    const docs: any[] = await mongo.withClient(async (client, db) => {
    const collection = db.collection('my_collection')

    return collection.find({}).toArray()
    })

    console.log(docs)

    Type Parameters

    • TResult extends unknown = any

    Parameters

    Returns Promise<TResult>

    The promise with the result.

  • Creates and opens a new MongoDatabase instance.

    Example

    import MongoDatabase from '@egomobile/mongo'

    const mongo = await MongoDatabase.open()

    // (true)
    mongo.isConnected

    await mongo.disconnect()

    // (false)
    mongo.isConnected

    Parameters

    Returns Promise<MongoDatabase>

    The new and open connection.

Generated using TypeDoc