mongodb MWE

This commit is contained in:
Chris Kanich
2020-02-10 19:56:20 -06:00
parent 7262fc518a
commit 971c154cf2
3 changed files with 179 additions and 0 deletions

27
mongo/client.js Normal file
View File

@@ -0,0 +1,27 @@
const { MongoClient } = require("mongodb");
const PORT = process.env.PORT || 27017;
const HOST = process.env.HOST || 'localhost';
async function main() {
const client = new MongoClient(`mongodb://${HOST}:${PORT}/dbName`);
try {
await client.connect();
await client
.db()
.collection("exampleCollection")
.insertOne({
someKey: "someVal"
});
const result = await client
.db()
.collection("exampleCollection")
.findOne({});
console.log(JSON.stringify(result));
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);