Mongo-DB is a MongoDB wrapper using ES7 and Promises.
Installation
You can install Mongo-DB through the command line by using the following command:
npm i mongo-db --save
Usage:
import mongodb from ' mongo-db ' ;
const cfg = {
database : ' database ' ,
collection : ' database ' ,
user : ' database ' ,
password : ' database ' ,
host : ' mongo.local ' ,
port : 27017 ,
} ;
const url = ` mongodb:// ${ cfg . user } : ${ cfg . password } @ ${ cfg . host } : ${ cfg . port } / ${ cfg . database } ` ;
const db = new mongodb ( url , cfg . collection , true ) ;
Documentation:
db.find()
Find documents in the database
db . find ( )
. then ( ( result ) => {
console . log ( ` Documents found: ${ result } ` )
} )
. catch ( ( err ) => {
console . error ( ` Could not find documents: ${ err } ` )
} ) ;
db.findOne()
Find a document in the database
db . findOne ( )
. then ( ( result ) => {
console . log ( ` Document found: ${ result } ` )
} )
. catch ( ( err ) => {
console . error ( ` Could not find document: ${ err } ` )
} ) ;
db.insert()
Inserts a document into the database
const document = {
one : 1 ,
two : 2
} ;
db . insert ( document )
. then ( ( ) => {
console . log ( ' Document inserted! ' )
} )
. catch ( ( err ) => {
console . error ( ` Inserting the document failed: ${ err } ` )
} ) ;
db.insertMany()
Inserts documents into the database
const documents = [
{
one : 1 ,
two : 2
} ,
{
three : 3 ,
four : 4
}
] ;
db . insertMany ( documents )
. then ( ( ) => {
console . log ( ' Documents inserted! ' )
} )
. catch ( ( err ) => {
console . error ( ` Inserting the documents failed: ${ err } ` )
} ) ;
db.update()
Update a document in the database
const document = [
{
one : 1 ,
two : 2
}
] ;
db . update ( query , document , options )
. then ( ( ) => {
console . log ( ' Document updated! ' )
} )
. catch ( ( err ) => {
console . error ( ` Updating the document failed: ${ err } ` )
} ) ;
db.remove()
Remove a document in the database
db . remove ( query )
. then ( ( ) => {
console . log ( ' Document removed! ' )
} )
. catch ( ( err ) => {
console . error ( ` Removing the document failed: ${ err } ` )
} ) ;
db.aggregate()
Execute an aggregation framework query
db . aggregate ( query )
. then ( ( ) => {
console . log ( ' Aggregation framework query executed! ' )
} )
. catch ( ( err ) => {
console . error ( ` Executing aggregation framework query failed: ${ err } ` )
} ) ;
db.count()
Count documents in collection
db . count ( )
. then ( ( result ) => {
console . log ( ` Document count: ${ result } ` )
} )
. catch ( ( err ) => {
console . error ( ` Counting the documents failed: ${ err } ` )
} ) ;
db.countByQuery()
Count documents in collection by query
db . countByQuery ( query )
. then ( ( result ) => {
console . log ( ` Document count: ${ result } ` )
} )
. catch ( ( err ) => {
console . error ( ` Counting the documents failed: ${ err } ` )
} ) ;
db.createIndex()
Create a index
db . createIndex ( index , options )
. then ( ( ) => {
console . log ( ' Index created! ' )
} )
. catch ( ( err ) => {
console . error ( ` Creating the index failed: ${ err } ` )
} ) ;
Contributing:
Interested in contributing to Mongo-DB? Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.
License:
Code licensed under MIT , documentation under CC BY 3.0 .