skip to Main Content

Here is the code example:

app.js

const express = require("express");
const app = express();
const cors = require("cors");
require("dotenv").config({ path: "./config.env" });
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
app.use(require("./routes/record"));
// get driver connection
const dbo = require("./db/conn");
 
app.listen(port, () => {
  // perform a database connection when server starts
  dbo.connectToServer(function (err) {
    if (err) console.error(err);
 
  });
  console.log(`Server is running on port: ${port}`);
});

db/conn.js

const { MongoClient } = require("mongodb");
const Db = process.env.ATLAS_URI;
const client = new MongoClient(Db, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
 
var _db;
 
module.exports = {
  connectToServer: function (callback) {
    client.connect(function (err, db) {
      // Verify we got a good "db" object
      if (db)
      {
        _db = db.db("employees");
        console.log("Successfully connected to MongoDB."); 
      }
      return callback(err);
         });
  },
 
  getDb: function () {
    return _db;
  },
};

I’ve done some research and it stands for ‘Database Owner’ in SQL Server but it doesn’t make sense here.

These code examples are from MongoDB official documentation and why do they assign connection instance as dbo if it really stands for Database Owner?

3

Answers


  1. As the documentation provides no explanation for the choice of variable name, we can only guess. My best guess is that dbo stands for database object, considering the type of the export.

    Login or Signup to reply.
  2. There is no restriction and built-in parameters name in nodejs.

    You can use that according to your preference

    Login or Signup to reply.
  3. I think this is idiosyncratic usage mimicked from the SQL world where dbo is the default schema and abbreviates database owner.

    Typically, and in the MongoClient documentation, I see just db = require("./db/conn"); But it appears here that the author’s fingers are just used to automatically typing an o after typing db.

    Humans are habitual imitators. I wouldn’t think too much of it. It’s just a variable name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search