Initializes a new instance of that class.
Optional
optionsOrFunc: GetMongoDatabaseOptions | IMongoDatabaseOptions = defaultGetMongoDatabaseOptionsCustom options or the function, that provides it.
Protected
_clientThe current connect.
Readonly
getThe function, that returns the options for that instance.
Gets if instance is connected or not.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// (true)
mongo.isConnected
await mongo.disconnect()
// (false)
mongo.isConnected
Is connected or not.
Creates a types collection instance by name.
The name of the collection.
The new instance.
Does a count on a MongoDB collection.
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' })
The collection's name.
Optional
filter: Filter<T>The filter.
Optional
options: CountDocumentsOptionsCustom options.
The promise with the number of documents.
Create an index on a collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
await mongo.createIndex('my_collection', {
foo: 1,
bar: -1
})
The collection's name.
The field or spec.
Optional
options: CreateIndexesOptionsCustom options.
The promise with the result.
Delete documents from a MongoDB collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// delete all documents with foo === 1
await mongo.deleteMany('my_collection', {
foo: 1,
})
The collection's name.
The filter.
Optional
options: DeleteOptionsCustom options.
The promise with the result.
Delete a document from a MongoDB collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// delete first document with foo === 1
await mongo.deleteOne('my_collection', {
foo: 1,
})
The collection's name.
The filter.
Optional
options: DeleteOptionsCustom options.
The promise with the result.
Registers the process events to close the connect on exit.
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)
The custom exit code.
This instance.
Does a find on a MongoDB collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// all, where foo === 1
const docs: any[] = await mongo.find('my_collection', {
foo: 1,
})
The collection's name.
The filter.
Optional
options: FindOptions<T>Custom options.
The promise with the result.
Does a findOne on a MongoDB collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// (null) if not found
const doc: any = await mongo.findOne('my_collection', {
foo: 1,
})
The collection's name.
The filter.
Optional
options: FindOptions<T>Custom options.
The promise with the result or (null) if not found.
Insert many documents into a MongoDB collection.
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,
}])
The collection's name.
The documents to insert.
Optional
options: BulkWriteOptionsCustom options.
The promise with the result.
Insert one document into a MongoDB collection.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// insert a single document
await mongo.insertOne('my_collection', {
foo: 1,
})
The collection's name.
The document to insert.
Optional
options: BulkWriteOptionsCustom options.
The promise with the result.
Update documents in a MongoDB collection.
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
}
})
The collection's name.
The filter for the documents.
The update query for the documents.
Optional
options: UpdateOptionsCustom options.
The promise with the result.
Update one document in a MongoDB collection.
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
}
})
The collection's name.
The filter for the document.
The update query for the document.
Optional
options: UpdateOptionsCustom options.
The promise with the result.
Opens a new client connection if it doesn't exist and executes an action on it.
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)
The action to invoke.
The promise with the result.
Static
openCreates and opens a new MongoDatabase instance.
import MongoDatabase from '@egomobile/mongo'
const mongo = await MongoDatabase.open()
// (true)
mongo.isConnected
await mongo.disconnect()
// (false)
mongo.isConnected
Optional
optionsOrFunc: GetMongoDatabaseOptions | IMongoDatabaseOptions = defaultGetMongoDatabaseOptionsThe options or the function that provides it.
The new and open connection.
Generated using TypeDoc
A connection to a MongoDB database.