How do I use node-mongodb-native to connect to Modulus.io?

First question here, so be kind ;)

I am configuring a Node.js server to connect to a MongoDB database in Modulus.io node.js hosting (really good stuff, worth checking it out), but I can't seem to properly stablish connection. Per the getting-started guide I get a connection uri in the format:

mongodb://user:pass@mongo.onmodulus.net:27017/3xam913

But that doesn't seem to work with the structure of the code I was trying to port to the server (had it running locally) because of the Server class argument structure with only host and port to define...

This is the code I am trying to adapt to the connection:

// server setup
var mongo = require('mongodb'),
    mdbServer = mongo.Server,
    mdbDb = mongo.Db,
    mdbObjectID = mongo.ObjectID;

// open a connection to the mongoDB server
var mdbserver = new mdbServer('localhost', 27017, {auto_reconnect: true});

// request or create a database called "spots03"
var db = new mdbDb('spots03', mdbserver, {safe: true});

// global var that will hold the spots collection
var spotsCol = null;

// open the database
db.open(function(err, db) {
    if(!err) {
        // if all cool
        console.log("Database connection successful");

        // open (get/create) a collection named spotsCollection, and if 200, 
        // point it to the global spotsCol
        db.createCollection(
            'spotsCollection',
            {safe: false},  // if col exists, get the existing one
            function(err, collection) {spotsCol = collection;}
        );
    }
});

Any help would be much appreciated, thanks!

Answers


Looks like a couple of things:

  1. The connection URL should be mongo.onmodulus.net

    var mdbserver = new mdbServer('mongo.onmodulus.net', 27017, {auto_reconnect: true});

  2. rounce is correct, the database name is auto-generated by Modulus.

    var db = new mdbDb('3xam913', mdbserver, {safe: true});

  3. Modulus databases will need authentication. Before you call createCollection, you'll have to call auth and pass it the user credentials that are setup on the project dashboard.

I'm a Modulus developer, and I know the DB name thing is not ideal.

Edit: here's full source for a working example. It records every HTTP request and then sends all requests back to the user.

var express = require('express'),
      mongo = require('mongodb'),
     Server = mongo.Server,
         Db = mongo.Db;

var app = express();

var server = new Server('mongo.onmodulus.net', 27017, { auto_reconnect: true });
var client = new Db('piri3niR', server, { w: 0 });
client.open(function(err, result) {
  client.authenticate('MyUser', 'MyPass', function(err, result) {
    if(!err) {
      console.log('Mongo Authenticated. Starting Server on port ' + (process.env.PORT || 8080));
      app.listen(process.env.PORT || 8080);
    }
    else {
      console.log(err);
    }
  });
});

app.get('/*', function(req, res) {
  client.collection('hits', function(err, collection) {
    collection.save({ hit: req.url });

    // Wait a second then print all hits.
    setTimeout(function() {
      collection.find(function(err, cursor) {
        cursor.toArray(function(err, results) {
          res.send(results);
        });
      });
    }, 1000)
  });
});

Need Your Help

Jquery Unable to Hover append html

javascript jquery ajax hover append

I am appending HTML into the DOM via jquery.append - my script is as follows (please excuse the crappy code)

React img tag issue with url and class

javascript css reactjs

I have the following simple react code in my JSX file:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.