Currently building an app with express and mongoDB.I find myself constantly questioning multiple things which I couldn't find a clear answer on. When we have the following (simplified) application:
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
const express = require('express')
const app = express()
app.get('/', (req, res) => {
MongoClient.connect(url,(err, db) => {
if (err) throw err;
db.db("mydb").collection("customers").findOne({}, (err, result) => {
if (err) throw err;
// do useful stuff
db.close();
});
});
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
Questions:
- What is the most 'expensive' operation, is this opening the DB or doing the action on the DB like findOne/updateOne. (or is it more complicated than this?)
- If 2 user agents send a get request to
/
and the get route is executed twice does the mongoDB database makes 2 connections or can it do this on the same connection?