Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs Mongoose + Mongo, connecting to localhost

I just updated to node 0.5.10 from 0.4.11 and my version of mongoose from 2.0.2 to 2.3.13 however I can no longer connect to localhost. My code has not changed at all, and I can still connect to my production server on mongolab. I can connect to my local db using a gui (MongoHub) just going through localhost. I am now running out of ideas. I've tried variations on the following connection strings which both should work in my option.

mongodb://localhost:27017/mydb
mongodb://localhost/mydb

I have even created a very basic single app to just save something to my localhost but to no avail. Ideas are very welcome!

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/SomeDb');

var Schema = mongoose.Schema;
var Posts = new Schema({
  name : String,
});
mongoose.model('Post', Posts);

function createNewPost(){
    var Post = mongoose.model('Post');
    var post = new Post({name:'new name'});
    post.save(function(err){
      console.log("saving");
        if(!err){
            console.log('Post saved.');
        }
    });
}
like image 603
henry.oswald Avatar asked Nov 19 '11 00:11

henry.oswald


People also ask

How do I access MongoDB on localhost?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

How does MongoDB connect to local node?

Connecting MongoDB The following example demonstrates connecting to the local MongoDB database. var MongoClient = require('mongodb'). MongoClient; // Connect to the db MongoClient. connect("mongodb://localhost:27017/MyDb", function (err, db) { if(err) throw err; //Write databse Insert/Update/Query code here.. });

Which method is used to connect MongoDB instance using Mongoose?

You can connect to MongoDB with the mongoose. connect() method. mongoose. connect('mongodb://localhost:27017/myapp');


1 Answers

After debugging using the mongodb native I found that 127.0.0.1 works. I have not idea but I am back up and running.

mongodb://127.0.0.1/mydb
like image 92
henry.oswald Avatar answered Oct 02 '22 00:10

henry.oswald