0

I am very new to MongoDB and node. So first of all:

  • How can I install MongoDB using the terminal on a Mac computer
  • Then how can I connect MongoDB with node?
  • How can I create a database in MongoDB using node?

    Any help is appreciated. Thank You.

    var MongoClient = require('mongodb').MongoClient;   
    var url = "mongodb://localhost:8080/mydb";
    MongoClient.connect(url, function(err, db){
    if(err)
    {
      console.log('error');
      throw err;
    }
    else
    {
       console.log('success');
    }
    });
    

I tried this code but it shows an error.

This is the error I received:

T

3
  • have you started the mongodb service ? Commented Aug 31, 2018 at 5:07
  • You have to start mongo server in your computer and then only you will able to connect it to the client. Commented Aug 31, 2018 at 5:38
  • Default port for mongodb is 27017 so your url should be like mongodb://localhost:8080/mydb Commented Aug 31, 2018 at 7:40

1 Answer 1

1

You have to open the MongoDB server first.

Do this:

  1. Go to the directory where mongo is installed: i.e.: <mongodb-install-directory>/bin

  2. Start the server by : ./mongod --dbpath <mongo-data-directory>

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

2 Comments

how can I know where I installed mongoDB
Thank you I got it.

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.