Skip to content

Instantly share code, notes, and snippets.

@mesaglio
Created February 7, 2020 19:44
Show Gist options
  • Save mesaglio/b6355fbb7f4cb4759253869e2cf98355 to your computer and use it in GitHub Desktop.
Save mesaglio/b6355fbb7f4cb4759253869e2cf98355 to your computer and use it in GitHub Desktop.
singleton mongo connector
var MongoClient = require('mongodb').MongoClient
var db;
module.exports = {
connect: connect
};
async function connect(url) {
if (db) return db;
const client = await MongoClient.connect("mongodb://localhost:27017/", { useUnifiedTopology: true });
db = client.db("estudio");
return db;
}
/**
* to use
*/
mongo.connect().then(client => client.collection("apellido").find({}, { projection: { _id: 0 } }).toArray()
.then(data => res.status(200).json(data)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment