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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:57:17+00:00 2026-05-21T15:57:17+00:00

I am new to both Node.js and MongoDB, but I intend to create a

  • 0

I am new to both Node.js and MongoDB, but I intend to create a very basic real time geolocation based web app. Here is my attempt at figuring out how Node and MongoDB interact:

var mongo = require('mongodb');

var db = new mongo.Db('test', new mongo.Server('localhost',22892, {}), {});

db.open(function(){});

db.collection('docs', function(err,collection){
    doc = {"foo":"bar"};
    collection.insert(doc, function(){});
});

I can see that this is connecting:

Thu Apr 14 15:24:12 [initandlisten] connection accepted from 127.0.0.1:46968 #26
Thu Apr 14 15:24:12 [conn26] building new index on { _id: 1 } for test.docs
Thu Apr 14 15:24:12 [conn26] done for 0 records 0secs

But it’s not inserting any documents into the database. Can anyone tell me what I am doing wrong?

Thanks

  • 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-05-21T15:57:18+00:00Added an answer on May 21, 2026 at 3:57 pm
    db.open(function(err, client){
        client.createCollection("docs", function(err, col) {
             client.collection("docs", function(err, col) {
                 for (var i = 0; i < 100; i++) {
                     col.insert({c:i}, function() {});
                 }
             });
        });
    });
    

    You forgot to do everything in your open callback. This is important otherwise your code runs before your connection to the database is open. You have to do everything asynchronous. It’s also best to create the collection if it does not exist.

    Take a look at the extensive examples at the github page

    Now this looks like callback spaghetti so we use flowcontrol like Step to make it pretty.

    Step(
        function() {
            db.open(this);
        },
        function(err, client) {
            client.createCollection("docs", this);
        },
        function(err, col) {
            for (var i = 0; i < 100; i++) {
                col.insert({c:i});
            }
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to both Ruby and Rails but I'm book educated by now (which apparently
I'm new to both Web Services and RMI and I wonder which is the
I'm very new to both the Mvc framework as well as JavaScript and JQuery.
I am working with Node.js to build a web socket server that uses mongodb.
I have a new web app that is packaged as a WAR as part
I'm new to both ASP.Net MVC and jQuery and what I'm trying to do
I'm new to both C# and Linq. I'm attempting to use a Linq-generated element
So, I'm reasonably new to both unit testing and mocking in C# and .NET;
I'm looking at a new project where both Native and Managed approaches sound like
I'm confused by the discussion and advancement both of a new version of HTML

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.