2

So i am trying to use Node.js and Mongodb together and the goal is to use Node to get information and store it in a database with Mongodb. SO I have both Node and Mongdb intalled, and I install the Mongodb package with npm, this is the package mongodb recommends. But the problem I am having is that when I try to do

    MongoClient.connect("mongodb://localhost:3000/exampleDb", function(err, db) {
    if(err) { return console.dir(err); }else{
    var collection =db.createCollection('test', function(err, collection) {}); }});

and I go to localhost:port_for express_server, but when the above code is supposed to run I get [Error: failed to connect to [localhost:3000]] in the Node console.Am I supposed to be running mongodb in the background or how is this supposed to work?

1
  • 2
    MongoDB usually listens on port 27017, not 3000. Commented Mar 9, 2013 at 9:13

2 Answers 2

2

when you do

npm install mongodb

you only install a node.js client driver for mongodb.

in order for you script to run, you need to install and start a mongodb server on your box

check server installation procedures on http://docs.mongodb.org/manual/installation/

Sign up to request clarification or add additional context in comments.

1 Comment

and i do have mongodb installed on my computer, just dont know how tto start the mongodb server on the port i want so that it matches the port i used within my node script
1

You seem pretty lost on what mongodb is and how to use it. Mongodb is a noSQL database.

Am I supposed to be running mongodb in the background

Yes, like mysql, you need the server to be installed and running, to use it. You need to do:

  1. mongodb (the db server)
    • install server
    • start the server by typing mongod on terminal
    • check with mongo if it is running and you can connect to it.
  2. node.js (the web server)
    • install node
    • install mongodb package
    • now test your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.