Files
example-services/mongodb/client.js

30 lines
695 B
JavaScript
Raw Normal View History

2020-02-10 19:56:20 -06:00
const { MongoClient } = require("mongodb");
2020-02-10 20:02:20 -06:00
const PORT = process.env.MONGODB_PORT || 27017;
2020-02-10 20:16:08 -06:00
const HOST = process.env.MONGODB_HOST || "localhost";
2020-02-10 19:56:20 -06:00
async function main() {
2020-02-10 20:16:08 -06:00
const client = new MongoClient(`mongodb://${HOST}:${PORT}/dbName`, {
useUnifiedTopology: true
});
2020-02-10 19:56:20 -06:00
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);