betacar
betacar

Reputation: 426

Meteor does not connect to MongoDB

I'm trying to connect Meteor to an existing MongoDB. I can't duplicate the database or change its name, because is used by other app.

I know I have to set a MONGO_URL environment var to connect with it. However, after I set it, Meteor is not connecting to the especiefied MongoDB database. I tried doing a .find() but it does not return any docs. An .insert() from the web console shows the info in the page, but it doesn't get inserted in the database. Here are the codes:

$ echo $MONGO_URL
mongodb://localhost:27017/autana_dev

./lib/models.js

Posts = new Meteor.Collection('posts');

./server/app.js

Meteor.publish('posts', function() {
  return Posts.find();
});

./client/app.js

Meteor.subscribe('posts');

Template.main.posts = function() {
  return Posts.find();
};

Any idea? Anyone? My Meteor version release is 0.6.4.1, and the MongoDB version is 2.4.1.

UPDATE: July 28 After running meteor with meteor run, I opened a new console window within project directory to run a meteor mongo console. However, after running meteor mongo I received the following:

mongo: Meteor isn't running.

This command only works while Meteor is running your application
locally. Start your application first.

Upvotes: 6

Views: 9615

Answers (4)

foobarbecue
foobarbecue

Reputation: 7060

As you know, because you posted the issue, this is a bug in meteor.

My workaround was to connect to my mongodb with the standard mongo client. You can find what port your db is running on by looking at the file yourapp/.meteor/db/METEOR-PORT and then just run mongo localhost:[put that port number here] .

Upvotes: 3

lutinfou
lutinfou

Reputation: 1

First, run mongo in one shell's windows

Then simply run "meteor mongo" in another shell's windows while the fist is still running ! On my mac, in the "leaderboard exemple", here is how i've done :

  1. "meteor run leaderboard "
  2. "cd .meteor/leaderboard;meteor mongo "

Upvotes: 0

Micha Roon
Micha Roon

Reputation: 4007

I am assuming you run Meteor on Linux or Mac. If this is not the case, the code might not work.

the most obvious reason is that the mongo database does not contain any posts documents. What is the output of:

$ mongo
> use autana_dev
> db.posts.find({}).count()

If the output is not null or 0 you might need to connect with a username and password.

export MONGO_URL="mongodb://user:password@localhost:27017/autana_dev

if this does not help, I'd try adding a bit of logging to find out if posts are not sent or not received:

Meteor.publish('posts', function() {
   console.log("returning " + Posts.find({}).count() + " posts");
   return Posts.find();
});

and if the number is greater than 0 on the client

var postsHandle = Meteor.subscribe('posts');

Template.main.posts = function() {
  if( postsHanlde.ready() ){
    console.log("I have " + Posts.find({}).count() + " posts on the client");
    return Posts.find();
  }
};

the condition if( postsHanlde.ready() ) ensures you do not try to show the posts before they have arrived.

If you have documents in your mongodb but nothing gets selected in Meteor, maybe your mongodb is not working correctly.

It is fairly easy to check that by replacing the mongodb with an online solution. Create a free account on mongolab and change your MONGO_URL to the one given by the mongolab console. Insert some posts there and test again.

If you still do not get any documents, then ...

That is all I can think of from your input. Hope it helped.

Upvotes: 0

jrullmann
jrullmann

Reputation: 2978

Are you sure the problem is that Meteor doesn't connect to the database? You have several pieces here that have to work together. I don't see anything obviously wrong, but I would simplify the code to verify the problem is where you think it is.

For example, try adding console.log(Posts.find()) to Meteor.startup on the server.

Upvotes: 0

Related Questions