Created
March 31, 2020 08:45
-
-
Save ehpc/3af7984cf8f5474aa8fee669ce963bf6 to your computer and use it in GitHub Desktop.
Node.JS MongoDb Async/Await
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
const MongoClient = require('mongodb').MongoClient; | |
const uri = 'mongodb://localhost:27017/'; | |
const mongoClient = new MongoClient(uri, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
(async () => { | |
const connection = await mongoClient.connect(); | |
const ducksDb = connection.db('ducks'); | |
const ducks = await ducksDb.createCollection('ducks'); | |
await ducks.insertOne({ color: 'yellow' }); | |
const yellowDuck = await ducks.findOne({ color: 'yellow' }); | |
console.info('MY YELLOW DUCK', yellowDuck); | |
connection.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment