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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:25:18+00:00 2026-06-15T15:25:18+00:00

The following code is working, in that it returns the document in the console:

  • 0

The following code is “working”, in that it returns the document in the console:

var Db = require('mongodb').Db;
var mongoUri = 'mongodb://localhost:27017/basketball';

exports.games = function(req, res){
    console.log(req);
    res.end("list of games");
    Db.connect(mongoUri, function(err, db) {
        console.log('connected!');
        db.collection('game_ids', function(err, coll) {
            coll.findOne({'date' : '2012-12-07'}, function(err, doc) {
                console.log(doc);
            });
        });
        db.close();
    });
};

>
connected!
{ _id: 50b01b31597f14213a00010f,
  date: '2012-12-07',
  espn_id: '400277990',
  hid: '20',
  aid: '2',
  home: '76ers',
  away: 'Celtics',
  season: '2013',
  during: 'regular',
  scrape: null }

But when I look at the mongod console, I see that every time I refresh the page, it seems to open more and more connections without closing them. Here you can see after 5 refreshes, I now have 25 open connections apparently (you have to scroll to the right to see the numbers):

Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57587 #121 (21 connections now open)
Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57588 #122 (22 connections now open)
Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57589 #123 (23 connections now open)
Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57590 #124 (24 connections now open)
Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57591 #125 (25 connections now open)

What am I doing wrong?

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

    You’re attempting to close the connection while the work is being done.

        // the work is done in the call back functions
        db.collection('game_ids', function(err, coll) {
            coll.findOne({'date' : '2012-12-07'}, function(err, doc) {
                console.log(doc);
            });
        });
    
        // the work above with the callback is async, this executes
        // immediately after initiating that work.
        db.close();
    

    If you’re going to open and close in every call, you would close after the work is done (after the console.log call in your case).

    You may also want to look into connection pooling so you don’t have to open/close on every call. More here:

    http://technosophos.com/content/nodejs-connection-pools-and-mongodb

    You should also do error checking by checking the err in the callback functions. for example, if getting the collection failed, the findOne shouldn’t be done etc…

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

Sidebar

Related Questions

I wrote the following code for a project that I'm working on: var clicky_tracking
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
The following code is working great in a normal console application: private void button1_Click(object
I have the following code: function $(id) { return document.getElementById(id); } var material =
I'm trying to recreate the following code with jQuery: var node = document.createElement(div); node.innerHTML
I have the following code below, that is working for the most part. It
I have the following code: $(document).ready(function(){ $(.uiCloseButton).click(function(e) { e.preventDefault(); var element = $(this).parents('.uiOverlay'); alert(element);
In YUI I have following code working for mouse wheel. How do I make
We have the following code working for a complex rails form with checkboxes. I'm
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);

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.