6

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.
Community
  • 1
  • 1
betacar
  • 416
  • 2
  • 9
  • 21

4 Answers4

3

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] .

foobarbecue
  • 6,780
  • 4
  • 28
  • 54
0

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.

jrullmann
  • 2,912
  • 1
  • 25
  • 27
  • `console.log(Posts.find());` returns an object with no collections nor docs: `Object { collection={...}, cursor_pos=0, reactive=true}`; `Object { docs={...}, _observeQueue={...}, next_qid=2, more...}` – betacar Jul 29 '13 at 15:31
  • I'm having the same problem as you are all of a sudden @betacar but my console.log defaults to a `LocalCollection` for some reason - although I explicitly used a name: `LocalCollection.Cursor {collection: LocalCollection,` – Jason J. Nathan Nov 17 '13 at 11:13
  • Try console.log(Posts.find().count()) instead. Find doesn't immediately access the database or return documents. – jrullmann Nov 18 '13 at 15:53
0

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.

Micha Roon
  • 3,957
  • 2
  • 30
  • 48
  • The `posts` collection contains nearly 2 million documents. Obviously is not the collection. My local MongoDB is not protected with user/pass authentication, because is intended to be use for development purposes. – betacar Jul 31 '13 at 14:23
  • there must be an error somewhere. did you try the logging part to find out if the data was being sent ? – Micha Roon Aug 01 '13 at 05:06
0

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 "