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 8351917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:47:43+00:00 2026-06-09T08:47:43+00:00

I am pretty new to Node.js and I am using the MongoDb driver, I

  • 0

I am pretty new to Node.js and I am using the MongoDb driver, I understand the workflow of Node.js is eventdriven and everything runs asynchronous but I just don’t understand how I can do things like this:

var mongo = require('mongodb');
var db = new mongo.Db('meta', new mongo.Server('localhost', 27017, { auto_reconnect: true }));

db.open(function(error, db){
  if(error){
    throw error;
  }

  db.collection('logs', function(error, collection){
    if(!collection){
      db.createCollection('logs', callback); // How does this callback works? I mean, how can we continue the previous execution of the script?
    }

    // Have fun.
  });
});

So with the script above I want to:

  1. Open the connection
  2. Check for logs collection, if not create, else continue…
  3. Create a user? Do work?

I am still getting used to the Asynchronous model, thanks for help already.

  • 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-09T08:47:44+00:00Added an answer on June 9, 2026 at 8:47 am

    What I usually do in this sort of scenario is wrap the continuation in a named callback:

    if (!collection) {
        db.createCollection('logs', next);
    } else {
        next(null, collection);
    }
    
    function next(err, collection) {
        // do something
    }
    

    That being said, the createCollection method will also return the collection object and only create it if it does not already exist. You can just write:

    db.createCollection('logs', function(err, collection) {
        // do something
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to Node.js/Express, but I think I'm slowly getting the hang of
I'm pretty new to XSLT, but need to use it for a CMS using
I am pretty new to CoffeeScript. I am trying to create Node.js application using
I'm pretty new to Node.js, so apologies in advance if I don't know what
Im pretty new to Java Web Services, but I cant find a good explanation
I am pretty new to PHP and MySQL and I just can't figure this
I'm pretty new to PHP, but I decided to try and make a simple
This is a pure best practice question. I am pretty new to Node and
I'm pretty new to the whole node.js thing (as well as servers, javascript and
I have a pretty basic REST call that is written using node / express

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.