Skip to content

Instantly share code, notes, and snippets.

@ehpc
Created March 31, 2020 08:45
Show Gist options
  • Save ehpc/3af7984cf8f5474aa8fee669ce963bf6 to your computer and use it in GitHub Desktop.
Save ehpc/3af7984cf8f5474aa8fee669ce963bf6 to your computer and use it in GitHub Desktop.
Node.JS MongoDb Async/Await
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