Integrating with MongoDB
Connection Setting
Set up a cluster from mongo atlas and copy url to driver
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
let _db;
const mongoConnect = callback => {
// database shop will be created automatically
MongoClient.connect(
'mongodb+srv://maximilian:9u4biljMQc4jjqbe@cluster0-ntrwp.mongodb.net/shop?retryWrites=true'
)
.then(client => {
console.log('Connected!');
_db = client.db();
callback();
})
.catch(err => {
console.log(err);
throw err;
});
};
const getDb = () => {
if (_db) {
return _db;
}
throw 'No database found!';
};
exports.mongoConnect = mongoConnect;
exports.getDb = getDb;CRUD Operation
Last updated
Was this helpful?