NodeJS - Connecting To MongoDB


Install dependencies.

npm init
npm install mongodb@2.2.5 --save

Download & Run MongoDB

sh ~/mongo/bin/mongod --dbpath ~/mongo-data
// 1. Load MongoDB library
// Get Mongo Client
const MongoClient = require('mongodb').MongoClient;

// 2. Connect to Database with MongoDB Client
MongoClient.connect('mongodb://localhost:27017/TodoApp', (error, db) => {
    // 3. If received an error, log error & return
    if (error) {
        return console.log('Unable to connect to MongoDB Server');
    }
    // 4. Log success message of MongoDB connected.
    console.log('Connected to MongoDB server');
    // 5. Insert a record to A collection named `ToDos`
    // 6. Pass a javascript object. In this case, object has text & completed.
    db.collection('Todos').insertOne({
        text: 'Something to do',
        completed: false
    // 7. Handle insertion response.
    }, (error, result) => {
        // 8. If error received do something.
        if (error) {
            return console.log('Unable to insert todo', error);
        }
        // 9. If record inserted successfully, do somehthing
        console.log(JSON.stringify(result.ops, undefined, 2));
    });
    // 10. Close the connection now
    db.close();
});

Download & launch Robo 3T You should see output as follows.

Robo3TImage

I agree, screenshot above & code given above are not matching. There you go.

    db.collection('Users').insertOne({
        name: 'Sagar R. Kothari',
        age: 30,
        location: 'Hyderabad, Telangana, India'
    }), (error, result) => {
        if (error) {
            return console.log('Unable to insert User', error);
        }
        console.log(JSON.stringify(result.ops, undefined, 2));
    }