Mongotoolbox
MongoDB toolbox for index management and simple connection to authorized database
Using
This utility requires:
- Node.js
>= 6.0.0
- mongodb driver
^2.2.4
Install package with npm:
npm i -S mongotools
Ensure indexes at collection
const mongotools = ; mongotools index someUniqueIndex: 1 sparse: true unique: true ;
Connect to database
const mongotools = ; mongotools ;
Connect to database and authenticate
const mongotools = ; const url = 'mongodb://localhost:27017';const options = user: 'username' password: 'foo' ; mongotools ;
API
connectAndAuth method
- connectAndAuth(url: string, options: object)
url
: mongodb connection stringoptions
options for mongodb connect methodoptions.user
: username for authenticationoptions.password
: password for authentication
EnsureIndex class
-
new EnsureIndex(mongoDbCollection)
- creates instance of mongodb collection
-
index(definition: string, options: object)
definition
: object accepted by mongooptions
: object accepted by mongo
-
writeIndexes()
- removes unwanted indexes and writes non-existing ones