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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:31:31+00:00 2026-06-09T20:31:31+00:00

I’m using the Node.JS driver for MongoDB, and I’d like to perform a synchronous

  • 0

I’m using the Node.JS driver for MongoDB, and I’d like to perform a synchronous query, like such:

function getAThing()
{
    var db = new mongo.Db("mydatabase", server, {});

    db.open(function(err, db)
    {
        db.authenticate("myuser", "mypassword", function(err, success)
        {
            if (success)
            {
                db.collection("Things", function(err, collection)
                {
                    collection.findOne({ name : "bob"}, function(err, thing)
                    {                           
                        return thing;
                    });
                });
            }
        });
    });
}

The problem is, db.open is an asychronous call (it doesn’t block), so the getAThing returns “undefined” and I want it to return the results of the query. I’m sure I could some sort of blocking mechanism, but I’d like to know the right way to do something like this.

  • 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-09T20:31:32+00:00Added an answer on June 9, 2026 at 8:31 pm

    There’s no way to make this synchronous w/o some sort of terrible hack. The right way is to have getAThing accept a callback function as a parameter and then call that function once thing is available.

    function getAThing(callback)
    {
        var db = new mongo.Db("mydatabase", server, {});
    
        db.open(function(err, db)
        {
            db.authenticate("myuser", "mypassword", function(err, success)
            {
                if (success)
                {
                    db.collection("Things", function(err, collection)
                    {
                        collection.findOne({ name : "bob"}, function(err, thing)
                        {       
                            db.close();                    
                            callback(err, thing);
                        });
                    });
                }
            });
        });
    }
    

    Node 7.6+ Update

    async/await now provides a way of coding in a synchronous style when using asynchronous APIs that return promises (like the native MongoDB driver does).

    Using this approach, the above method can be written as:

    async function getAThing() {
        let db = await mongodb.MongoClient.connect('mongodb://server/mydatabase');
        if (await db.authenticate("myuser", "mypassword")) {
            let thing = await db.collection("Things").findOne({ name: "bob" });
            await db.close();
            return thing;
        }
    }
    

    Which you can then call from another async function as let thing = await getAThing();.

    However, it’s worth noting that MongoClient provides a connection pool, so you shouldn’t be opening and closing it within this method. Instead, call MongoClient.connect during your app startup and then simplify your method to:

    async function getAThing() {
        return db.collection("Things").findOne({ name: "bob" });
    }
    

    Note that we don’t call await within the method, instead directly returning the promise that’s returned by findOne.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported

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.