Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:12:05+00:00 2026-06-15T23:12:05+00:00

I am currently rolling back from mongoose to node-mongodb-native. So I am quite new

  • 0

I am currently rolling back from mongoose to node-mongodb-native.

So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function.

docs:

var mongodb = require("mongodb"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    // here I can do my queries etc.
});

But how can I get access to the db object in the callback when I am in some route callback?
Currently the only idea I would have is wrapping the application into the callback:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

var app = new express();

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    app.get('/products', function(req, res, next) {
        db.collection('products', function(err, collection) {
            if (err) next(new Error(err));
            collection.find({}, function(err, products) {
                res.send(products);
            });
        });
    });

});

But I do not think this is the way it should meant to be?

Isn’t there the way to create a sync database connection call which I then can easily use through the whole application how it was by mongoose?

Regards bodo

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:12:05+00:00Added an answer on June 15, 2026 at 11:12 pm

    Db.open opens the connection to mongodb and returns a reference to itself. See here for the sourcecode: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L245

    All you want is to hold off on starting your express app listening on it’s port and receiving requests until your connection to mongodb has been established.

    So what you could do is this:

    var mongodb = require("mongodb"),
        express = require("express"),
        mongoServer = new mongodb.Server('localhost', 27017),
        dbConnector = new mongodb.Db('example', mongoServer),
        db;
    
    var app = new express();
    
    app.get('/products', function(req, res, next) {
      db.collection('products', function(err, collection) {
        if (err) next(new Error(err));
        collection.find({}, function(err, products) {
          res.send(products);
        });
      });
    });
    
    db_connector.open(function(err, opendb) {
      if (err) throw new Error(err);
      db = opendb;
      app.listen(3000);
    });
    

    What I’m not sure about though is whether this is a good idea. This solution doesn’t allow you to recreate your connection if there has been a connection break or you restarted your mongodb process. So while the above might work, it might be a better idea to create a method that will wrap the creation of a connection to mongodb.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an integration test of a service that is not rolling back
I am currently creating a business case for rolling out TFS 2010 as our
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I'm starting a new Activity and calling finish on a current one. Is
Currently working with converting SQLException error messages into messages that are more useful for
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
I am currently retrieving both a UserTransaction and a DataSource from a Weblogic 10.3
I know questions like this have been asked numerous times, but not quite this
So I'm running into an interesting issue implementing session ID rolling after log-in/change of
I am currently working with a device that has iPhone OS 2.2.1. And as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.