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
collectionName
string Name of the collectiondocuments
array 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
collectionName
string Name of the collectionmutableEntity
object Document to update in the collectiontimeStamp
boolean 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
collectionName
string Name of the collectionmutableEntity
object Properties that will be updated.upsert
boolean Default is false, if set to true it will create or update the document with the given set of properties.query
object 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
collectionName
string Name of the collectionquery
object Querysort
object 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
collectionName
string Name of the collectionquery
object Querylimit
number Limit to the query. By default there’s no limit until specified.projection
object 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
db
objectcollectionName
string Name of the collectionquery
object query objectprojection
object fields to projectpageSize
number page size to return from the collection.processPage
function pass a function to handle the pagedResultsprocessPageArgs
array 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
db
objectcollectionName
string Name of the collectiondocuments
array Array of documents to be created
Returns any
bulkUpdate
Method to update documents bulk in a given collection
Parameters
db
objectcollectionName
string Name of the collectionupdates
array array of documents to updateomits
object 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
shutdown
any 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
name
string 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
db
db instance connectioncollectionName
Name of the collectionquery
Query on the basis of which documents will be picked from a collection.batchSize
Size of the batch you’d want to processprocessBatch
Function/method to run once desired docs are fetched from the DB.processBatchArgs
Additional 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
db
objectcollectionName
string Name of the collectionupdates
object Values that will be updated. Can update multiple values or set new values too.query
object Query to find the documents in the collection to update
Returns object