Table of Contents
- insertIntoDb
- updateDocument
- upsertDocument
- findOneDocumentBasedOnQuery
- findDocumentsBasedOnQuery
- countDocumentsByQuery
- workOnItPageByPage
- connectDb
- bulkCreate
- bulkUpdate
- registerForGracefulShutdown
- insertOne
- dropCollection
- findDistinctDocuments
- processABatchOfDocuments
- bulkUpdateByQuery
insertIntoDb
Method to insert documents in a given collection
Parameters
collectionNamestring Name of the collectiondocumentsarray Array of documents that will be inserted.
Returns object A document with acknowledged: true and an array of successfully inserted _id’s
updateDocument
Method to update a document in a given collection based on _id.
Parameters
collectionNamestring Name of the collectionmutableEntityobject Document to update in the collectiontimeStampboolean Default is false. If set to true it adds a key lastModifiedAt to the document with the current timestamp.
Returns any
upsertDocument
Method to upsert a document in a given collection
Parameters
collectionNamestring Name of the collectionmutableEntityobject Properties that will be updated.upsertboolean Default is false, if set to true it will create or update the document with the given set of properties.queryobject Default is querying by _id but a custom query can be specified.
Returns any
findOneDocumentBasedOnQuery
Method to find one document based on a given query
Parameters
collectionNamestring Name of the collectionqueryobject Querysortobject Sort, by default it sorts by _id.
Returns object an document if a match is found based on the query.
findDocumentsBasedOnQuery
Method to find documents based on query
Parameters
collectionNamestring Name of the collectionqueryobject Querylimitnumber Limit to the query. By default there’s no limit until specified.projectionobject Query Projection
Returns array an array of documents based on the query.
countDocumentsByQuery
Method to count documents based on query
Parameters
Returns number the count of documents based on a given query
workOnItPageByPage
Method to work on a collection page by page. PageSize can be even 1. Ideal if you want to work in batches.
This method requires you to connect to the DB first by using connectDb(). \n \n
Assumptions:
a) sorts will happen by _id in this method
b) query._id is overriden by this method
Parameters
dbobjectcollectionNamestring Name of the collectionqueryobject query objectprojectionobject fields to projectpageSizenumber page size to return from the collection.processPagefunction pass a function to handle the pagedResultsprocessPageArgsarray additional arguments required by processPage
connectDb
Method to connect to the db
Returns object db connection
bulkCreate
Method to create documents in bulk in a given collection.
Parameters
dbobjectcollectionNamestring Name of the collectiondocumentsarray Array of documents to be created
Returns any
bulkUpdate
Method to update documents bulk in a given collection
Parameters
dbobjectcollectionNamestring Name of the collectionupdatesarray array of documents to updateomitsobject Fields to omit while updating the documents in the collection
Returns any
registerForGracefulShutdown
Idea came from https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86
Parameters
shutdownany the calling code user this method to control what actions to take as part of shutdown
insertOne
Method to insert a single document.
Parameters
Returns any
dropCollection
Method to drop a collection
Parameters
namestring name of the collection to drop.
Returns any
findDistinctDocuments
Method to find distinct documents in a collection
Parameters
Returns array an array of field values that are in the collection
processABatchOfDocuments
Method to process a batch of documents in a collection
Parameters
dbdb instance connectioncollectionNameName of the collectionqueryQuery on the basis of which documents will be picked from a collection.batchSizeSize of the batch you’d want to processprocessBatchFunction/method to run once desired docs are fetched from the DB.processBatchArgsAdditional arguments that are required to be passed onto the processBatch method.
bulkUpdateByQuery
Method to bulk update documents in a collection given a specific query.
Parameters
dbobjectcollectionNamestring Name of the collectionupdatesobject Values that will be updated. Can update multiple values or set new values too.queryobject Query to find the documents in the collection to update
Returns object