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

  • SEARCH
  • Home
  • 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 8554949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:00:14+00:00 2026-06-11T15:00:14+00:00

It is unclear to me how to work that connection and query the collection.

  • 0

It is unclear to me how to work that connection and query the collection.
Because sometimes the connection is already there and there is no problem. But when the connection needs to be established, collection queries aren’t queued.
You cannot make a new connection all the time either, because you can (and should) make only one.
Seems like you need the collection query in a connection callback, but also stand-alone when the connection is already there.

Ofcourse, this isn’t undoable, but with all the blah and yaddah overhead, I imagine I am not doing it right.
After all, this seems kinda syntactic saccharin to me.
Where is the syntactic sugar?

Here’s a rudimentary example. Imagine a bunch of requests in the same second:

var util        = require('util'),
    mongo       = require('mongodb'),
    Server      = mongo.Server,
    Db          = mongo.Db,
    mServer     = new Server('localhost', 27017),
    mDb         = new Db('SomeDatabase', mServer),
    db;

if (mDb._state == 'disconnected') {
    util.debug('MONGODB: connecting...');
    mDb.open(function(err, newDb) {
        if (!err) {
            db = newDb;
            // I cannot immediately work on a collection here, 
            // because it takes a moment for the db to connect.
        }
    });
}
else if (mDb._state == 'connecting') {
    // I need to catch this state because I cannot work on collections yet, 
    // but I cannot connect either because it's already connecting.
}
// else if (mDb._state == 'connected') {

// Queries need to be performed after connection is established 
// or when connection was already established by a previous request.
db.createCollection(collection, function(err, collection) {
    if (!err) {
        collection.insert(record, {safe:true}, function(err, result) {
            util.debug('MONGODB: Inserted.');
        });
    }
});

// }

Now, the beginner guides I found are all about the callback on mDb.open, but this gives problems on successive requests when the node is already running. It refuses to open another connection. So, what is actually the correct way to play nice with mongodb in nodejs when a single launch callback isn’t enough?

I imagine a module that you pass queries in callback-form, which passes the callbacks around for you, either (1) to where it waits for the connection-callback, (2) directly to the collection, or (3) waiting for the connection, but too late for the connection-callback. But like I said, this is pure syntactic saccharin.

  • 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-11T15:00:15+00:00Added an answer on June 11, 2026 at 3:00 pm

    You misunderstand something. First you create the connection to DB:

    var mDb = new Db('SomeDatabase', mServer);
    

    and you hold the reference to this connection. Whenever you want to load/save something from/to DB, you use the same variable. You don’t recreate it on every request. Initialization should be done even before creating the web server itself ( create the server on open callback ). Here’s a pseudo code:

    var webserver = INITIALIZE_NEW_SERVER( );
    var mDB = new Db('SomeDatabase', mServer);
    
    // set request handler
    WHEN_REQUEST ---> {
        mDB.createCollection( ... );
        // whatever
    }
    
    mDb.open(function(error, newDb) {
        // handle errors
        webserver.listen( );
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for unclear subject but i really don't understand where problem. I use a
I want to use xui.js in a mobile web project that needs to work
I am still unclear about which adapter to use in a situation that doesn't
I'm a little unclear on the difference between reflow + repaint (if there's any
The manual is unclear as to how to implement this (it assumes you already
at work we have an application that, at first, had been developed for a
At work, we are developing an PHP application that would be later re-programmed into
I'm writing a program for some homework that's going to entail some forking, but
I'm dealing with some code at work that includes an expression of the form
I currently work on a trading application that does not use camel. It essentially

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.