mongodb-nodejs-sdk

An utility MongoDB library written in NodeJS. An end to end plausible solution to deal with MongoDB.


Table of Contents

insertIntoDb

Method to insert documents in a given collection

Parameters

  • collectionName string Name of the collection
  • documents 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 collection
  • mutableEntity object Document to update in the collection
  • timeStamp 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 collection
  • mutableEntity 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 collection
  • query object Query
  • sort 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 collection
  • query object Query
  • limit 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

  • collectionName string Name of the collection
  • query object Query object

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 object
  • collectionName string Name of the collection
  • query object query object
  • projection object fields to project
  • pageSize number page size to return from the collection.
  • processPage function pass a function to handle the pagedResults
  • processPageArgs 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 object
  • collectionName string Name of the collection
  • documents array Array of documents to be created

Returns any

bulkUpdate

Method to update documents bulk in a given collection

Parameters

  • db object
  • collectionName string Name of the collection
  • updates array array of documents to update
  • omits 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

  • collectionName string name of the collection
  • document object document to insert

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

  • collectionName string name of the collection
  • field string Distinct Field

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 connection
  • collectionName Name of the collection
  • query Query on the basis of which documents will be picked from a collection.
  • batchSize Size of the batch you’d want to process
  • processBatch 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 object
  • collectionName string Name of the collection
  • updates 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