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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:15:52+00:00 2026-06-08T14:15:52+00:00

When I run collection.find() in MongoDB/Node/Express, I’d like to get a callback when it’s

  • 0

When I run collection.find() in MongoDB/Node/Express, I’d like to get a callback when it’s finished. What’s the correct syntax for this?

 function (id,callback) {

    var o_id = new BSON.ObjectID(id);

    db.open(function(err,db){
      db.collection('users',function(err,collection){
        collection.find({'_id':o_id},function(err,results){  //What's the correct callback synatax here?
          db.close();
          callback(results);
        }) //find
      }) //collection
    }); //open
  }
  • 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-08T14:15:54+00:00Added an answer on June 8, 2026 at 2:15 pm

    That’s the correct callback syntax, but what find provides to the callback is a Cursor, not an array of documents. So if you want your callback to provide results as an array of documents, call toArray on the cursor to return them:

    collection.find({'_id':o_id}, function(err, cursor){
        cursor.toArray(callback);
        db.close();
    });
    

    Note that your function’s callback still needs to provide an err parameter so that the caller knows whether the query worked or not.

    2.x Driver Update

    find now returns the cursor rather than providing it via a callback, so the typical usage can be simplified to:

    collection.find({'_id': o_id}).toArray(function(err, results) {...});
    

    Or in this case where a single document is expected, it’s simpler to use findOne:

    collection.findOne({'_id': o_id}, function(err, result) {...});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a way to find out in run-time, whether a collection
This is a question for the generic collection gurus. I'm shocked to find that
I have this index a,b,-c on a Mongo collection. When I run this query:
This is a design problem I face regularly and I'd like to find some
I ran the following query on a collection in my mongodb database. db.coll.find({field_name:{$exists:true}}).count() and
I have over 300k records in one collection in Mongo. When I run this
Using Rails 3.0.7, REE 1.8.7. I want to run code (specifically, Garbage Collection) in
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
This is my thread: public void run() { Log.d(ConnectionThread,Starting Server Connection); try { while(isThereActivityRunning())
this is my thread: @Override public void run() { Log.d(ConnectionThread,Starting Server Connection); try {

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.