Creates a new instance of that class.
The database.
The underlying collection.
Readonly
collectionThe underlying collection.
Readonly
dbThe database.
Does a count on a MongoDB collection.
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' })
Optional
filter: Filter<T>The filter.
Optional
options: CountDocumentsOptionsCustom options.
The promise with the number of documents.
Create an index on that collection.
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
})
The field or spec.
Optional
options: CreateIndexesOptionsCustom options.
The promise with the result.
Delete documents from that collection.
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,
})
The filter.
Optional
options: DeleteOptionsCustom options.
The promise with the result.
Delete a document from that collection.
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,
})
The filter.
Optional
options: DeleteOptionsCustom options.
The promise with the result.
Does a find on that collection.
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,
})
The filter.
Optional
options: FindOptions<any>Custom options.
The promise with the result.
Does a findOne on that collection.
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,
})
The filter.
Optional
options: FindOptions<any>Custom options.
The promise with the result or (null) if not found.
Insert many documents into that collection.
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,
}])
The documents to insert.
Optional
options: BulkWriteOptionsCustom options.
The promise with the result.
Insert one document into that collection.
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,
})
The document to insert.
Optional
options: BulkWriteOptionsCustom options.
The promise with the result.
Update documents in that collection.
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
}
})
The filter for the documents.
The update query for the documents.
Optional
options: UpdateOptionsCustom options.
The promise with the result.
Update one document in that collection.
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
}
})
The filter for the document.
The update query for the document.
Optional
options: UpdateOptionsCustom options.
The promise with the result.
Generated using TypeDoc
A typed Mongo collection.