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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:04:01+00:00 2026-06-14T21:04:01+00:00

In a node.js server, using the mongodb native driver, I want to retrieve records

  • 0

In a node.js server, using the mongodb native driver, I want to retrieve records from a cursor and then output them as JSON. I have this (simplified)

var ans = {ids: []};
cursor.each(function(err, doc) {
 if (doc) {
  ans.ids.push(doc.tag);
 }
});
cursor.count(function(err, result) {
 ans.count = result;
 res.send(JSON.stringify(ans));
});

and the result is something like {ids:[], count: 3}. In other words the query appears to run without returning any records. I assume that this is because the data’s already been sent before the cursor.each callbacks have run. How do I re-structure this to make sure the sending happens after the iterating?

  • 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-14T21:04:02+00:00Added an answer on June 14, 2026 at 9:04 pm

    I have found the answer. The example for cursor.each says “If the item is null then the cursor is exhausted/empty and closed”, so: (error handling omitted)

    var ans = {ids: []};
    cursor.each(function(err, doc) {
     if (doc) {
      ans.ids.push(doc.tag);
     }
     else {
      cursor.count(function(err, result) {
       ans.count = result;
       res.send(JSON.stringify(ans));
      });
     }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote my first nodejs program using the node-mongodb-native driver. I used the
I am using node-mongodb-native driver. I tried collection.findOne({email: 'a@mail.com'}, function(err, result) { if (!result)
I am going to make a client-server application using Node.js, and I want to
I want to retrieve the last inserted _id , using mongoose as MongoDB wrapper
I am new to Node, and I'm using Mongoose as a driver for MongoDB.
I'm creating a small server using Node.js that I want to keep running with
I am running LDAP server using node.js (ldapjs.org) and i want to create a
When using the native mongo.db driver for node, should I open 1 connection per
I am pretty new to Node.js and I am using the MongoDb driver, I
I'm using the Node.JS driver for MongoDB, and I'd like to perform a synchronous

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.