Created
February 7, 2020 19:44
-
-
Save mesaglio/b6355fbb7f4cb4759253869e2cf98355 to your computer and use it in GitHub Desktop.
singleton mongo connector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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