ezmc - Easy Mongo Collection access and management
This module abstracts common database operations to provide a minimal API for data management. Using the base mongodb
drive one must go through a number of steps to perform a collection operation.
const MongoClient = MongoClient;const client = 'connectionString'; const countPromise = client; // Perform action on countcountPromise;
Mongo suggest that we only have one MongoClient per application, so we are also responsible for keeping this one client around. ezmc solves all of these problems and provides one line invocation for all collection methods.
const Ezmc = ;const client = 'connectionString' 'dbName' client ;
Requirements
- Node.js 4.4.x - 6.2.x
Install & use
First install ezmc
npm install --save ezmc
Secondly, to ensure there is only one instance for your entire application, we suggest that you create a file that exports an initialized ezmc. You then reference this file everywhere you want to use ezmc.
// mongoCollections.jsconst Ezmc = ; moduleexports = 'connectionStrings' 'dbName' options;
You can then use the same instance anywhere in your application
// index.jsconst mongoCollections = ; return mongoCollections ;
Motivation
We found ourselves writing the same lines of code over and over whenever we needed access to the collections. This introduced extra surface for errors and confusion. Secondly, it made testing a bit more difficult as we had to always stub out an extra layer or two just as setup. Finally, we did not have a consistent way of keeping one MongoClient around the entire application.
In order to keep things DRY we condensed our boilerplate code into one module. It's methods simply dereference the collection using it's single MongoClient and passes the arguments to the corrosponding Collection method.
API
Every methods first argument is the name of the collection you're referencing. The arguments following that will be passed directly to the Mongo collection. The Mongo Collection documentation (linked under each method here) will provided details on the parameters and return values. Every method returns a promise.
-
initializer (connectionString, options)
Mongo Docs -
close()
Close the database connection Mongo Docs
Standard Behaviour
bulkWrite(collectionName, operations, options)
Mongo Docscount(collectionName, query, options)
Mongo DocscreateIndex(collectionName, fieldOrSpecs, options)
Mongo DocscreateIndexes(collectionName, indexSpecs)
Mongo DocsdeleteMany(collectionName, filter, options)
Mongo DocsdeleteOne(collectionName, filter, options)
Mongo Docsdistinct(collectionName, key, query, options)
Mongo Docsdrop(collectionName)
Mongo DocsdropIndex(collectionName, indexName, options)
Mongo DocsdropIndexes(collectionName)
Mongo Docsfind(collectionName, query)
Mongo DocsfindOneAndDelete(collectionName, filter, options)
Mongo DocsfindOneAndReplace(collectionName, filter, replacement, options)
Mongo DocsfindOneAndUpdate(collectionName, filter, update, options)
Mongo DocsgeoHaystackSearch(collectionName, x, y, options)
Mongo DocsgeoNear(collectionName, x, y, options)
Mongo Docsgroup(collectionName, keys, condition, initial, reduce, finalize, command, options)
Mongo Docsindexes(collectionName)
Mongo DocsindexExists(collectionName, indexes)
Mongo DocsindexInformation(collectionName, options)
Mongo DocsinsertMany(collectionName, docs, options)
Mongo DocsinsertOne(collectionName, doc, options)
Mongo DocsisCapped(collectionName)
Mongo DocslistIndexes(collectionName, options)
Mongo DocsmapReduce(collectionName, map, reduce, options)
Mongo Docsoptions(collectionName)
Mongo DocsparallelCollectoinScan(collectionName, options)
Mongo DocsreIndex(collectionName)
Mongo Docsrename(collectionName)
Mongo DocsreplaceOne(collectionName, filter, doc, options)
Mongo Docsstats(collectionName)
Mongo DocsupdateMany(collectionName, filter, update, options)
Mongo DocsupdateOne(collectionName, filter, update, options)
Mongo Docs
Non-standard Behaviour
aggregate(collection, pipeline, options)
Mongo Docs Docs](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate)- Will return an array of results instead of a cursor unless
options.returnCursor
is true find(collection, query, options)
Mongo Docs- Will return an array of results instead of a cursor unless
options.returnCursor
is true findOne(collection, query, options)
Mongo Docs- Convience method. Simply calls
find
with the same arguments but addsoptions.limit = 1
- Returns the document if it's found, and null if it is not. Does not return an array.