Collection Methods
These MongoDB collection methods are made available if you need to test or run any method without hooks or Monastery argument/return processing.
These methods can be accessed via model._*
.
Returns
All methods return a promise.
Methods
- model._aggregate
- model._bulkWrite
- model._count
- model._createIndex
- model._createIndexes
- model._distinct
- model._drop
- model._dropIndex
- model._dropIndexes
- model._find
- model._findOne
- model._findOneAndDelete
- model._findOneAndUpdate
- model._indexes
- model._indexInformation
- model._insert
- model._remove
- model._stats
- model._update
model._aggregate
Execute an aggregation framework pipeline against the collection. Arguments:
Pipeline
(array)[Options]
(object)
await users.aggregate([
{ $project : { author : 1, tags : 1 }},
{ $unwind : "$tags" },
{ $group : { _id : { tags : "$tags" }}}
])
model._bulkWrite
Perform a bulkWrite operation without a fluent API. Arguments:
Operations
(array) - Bulk operations to perform[Options]
(object)
Legal operation types are:
await users._bulkWrite([
{ insertOne: { document: { a: 1 } } },
{ updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } },
{ updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } },
{ deleteOne: { filter: {c:1} } },
{ deleteMany: { filter: {c:1} } },
{ replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
])
model._count
Returns the count of documents that would match a find() query. The method uses collection.countDocuments() in the mongo driver. Arguments:
[Filter]
(string|objectId|object)[Options]
(object)
await users._count({ name: 'foo' })
await users._count('id') // a bit useless but consistent with the rest of the API
await users._count()
If you need to get a fast order of magnitude of the count of all documents in your collection, you can use the estimate option.
await users._count({}, { estimate: true }) // Filter is ignored
model._createIndex
Creates an index on the db and collection collection. Arguments:
IndexSpec
(string|array|object) - The field name or index specification to create an index for[Options]
(object)
await users.createIndex({ a: 1, b: -1 });
// Alternate syntax for { c: 1, d: -1 } that ensures order of indexes
await users.createIndex([ [c, 1], [d, -1] ]);
// Equivalent to { e: 1 }
await users.createIndex('e');
// Equivalent to { f: 1, g: 1 }
await users.createIndex(['f', 'g'])
// Equivalent to { h: 1, i: -1 }
await users.createIndex([ { h: 1 }, { i: -1 } ]);
// Equivalent to { j: 1, k: -1, l: 2d }
await users.createIndex(['j', ['k', -1], { l: '2d' }])
model._createIndexes
Creates multiple indexes in the collection. Arguments:
IndexSpec
*(array)*- The field names or index specifications to create an indexes for [Options]
(object)
await users.createIndexes([
// Simple index on field fizz
{ key: { fizz: 1 } }
// wildcard index
{ key: { '$**': 1 } },
// named index on darmok and jalad
{ key: { darmok: 1, jalad: -1 } name: 'tanagra' }
])
model._distinct
Returns a list of distinct values for the given key across a collection. Arguments:
Key
(string)[Filter]
(string|objectId|object)[Options]
(object)
await users._distinct('name')
model._drop
Drops the entire collection. Arguments: None
await users._drop()
model._dropIndex
Drops an index on the collection. Arguments:
IndexName
(string)[Options]
(object)
await users._dropIndex('name')
await users._dropIndex('name.last')
model._dropIndexes
Drops all indexes from the collection. Arguments: None
await users._dropIndexes()
model._find
Finds documents in the collection. Arguments:
[Filter]
(string|objectId|object)[Options]
(object)
await users._find()
await users._find({ name: 'John' })
await users._find({}, { projection: { name: 1 } }) // only the name field will be selected
await users._find({}, { rawCursor: true }) // returns raw mongo cursor
await users._find({}, {
stream: ((doc, { close, pause, resume }) => {
// the users are streaming here
// call `close()` to stop the stream
})
})
model._findOne
Fetches the first document that matches the filter. Arguments:
[Filter]
(string|objectId|object)[Options]
(object)
await users._findOne({ name: 'John' })
await users._findOne({}, { projection: { name: 1 } }) // only the name field will be selected
model._findOneAndDelete
Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. Arguments:
[Filter]
(string|objectId|object)[Options]
(object)
await users._findOneAndDelete({ name: 'John' })
model._findOneAndUpdate
Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation. Arguments:
Filter
(string|objectId|object)Update
(object)[Options]
(object)
await users._findOneAndUpdate({ name: 'John' }, { $set: { age: 30 } })
model._indexInformation
Retrieves this collections index info. Argument(s):
[Options]
(object)
await users._indexInformation()
model._indexes
Lists all indexes on the collection. Argument(s):
[Options]
(object)
await users._indexes()
model._insert
Inserts one or more documents into the collection. Arguments:
Doc
(object|array) - one or many documents[Options]
(object)
await users._insert({ name: 'John', age: 30 })
await users._insert([{ name: 'John', age: 30 }, { name: 'Bill', age: 32 }])
model._remove
Removes one or more document(s) from the collection. Arguments:
Filter
(string|objectId|object)[Options]
(object)
await users._remove({ name: 'John' })
model._stats
Returns statistics about the collection. Arguments:
[Options]
(object)
await users._stats()
model._update
Updates one or more document(s) from the collection. Arguments:
Filter
(string|objectId|object)Update
(object)[Options]
(object)
await users._update({ name: 'John' }, { $set: { age: 30 } })