Mongo-DB is a MongoDB wrapper using ES7 and Promises.
You can install Mongo-DB through the command line by using the following command:
npm i mongo-db --save
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);
db.find()
.then((result) => {
console.log(`Documents found: ${result}`)
})
.catch((err) => {
console.error(`Could not find documents: ${err}`)
});
db.findOne()
.then((result) => {
console.log(`Document found: ${result}`)
})
.catch((err) => {
console.error(`Could not find document: ${err}`)
});
const document = {
one: 1,
two: 2
};
db.insert(document)
.then(() => {
console.log('Document inserted!')
})
.catch((err) => {
console.error(`Inserting the document failed: ${err}`)
});
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}`)
});
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(query)
.then(() => {
console.log('Document removed!')
})
.catch((err) => {
console.error(`Removing the document failed: ${err}`)
});
db.aggregate(query)
.then(() => {
console.log('Aggregation framework query executed!')
})
.catch((err) => {
console.error(`Executing aggregation framework query failed: ${err}`)
});
db.count()
.then((result) => {
console.log(`Document count: ${result}`)
})
.catch((err) => {
console.error(`Counting the documents failed: ${err}`)
});
db.countByQuery(query)
.then((result) => {
console.log(`Document count: ${result}`)
})
.catch((err) => {
console.error(`Counting the documents failed: ${err}`)
});
db.createIndex(index, options)
.then(() => {
console.log('Index created!')
})
.catch((err) => {
console.error(`Creating the index failed: ${err}`)
});
Interested in contributing to Mongo-DB? Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.