Meteor does not connect to MongoDB - 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.

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

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.

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.

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 :
"meteor run leaderboard "
"cd .meteor/leaderboard;meteor mongo "

Related

Unable to get run js from command to query mongodb

Hi I am trying to run a simple JavaScript program to retrieve data from mongodb. collection named news.
var doc=db.news.findone();
printjson(doc);
I have mongodb running in my machine. when I try to run it from my command prompt, I am getting the below result.
MongoDB shell version:2.4.15-pre-
connecting to: test
I have no idea why it is connecting to test. Someone please help.
I assume, the collection, "news" resides on some mongo logical database, which need to be specified in script. For exmaple
db = db.getSiblingDB('db_name')
doc = db.news.findOne()
printjson(doc)
Then evaluate the script using
mongo --host ${host} --port ${port} script.js
If you don't specify a database in your connection string like for example,
//Connect to foo
${host}:${port}/foo
mongo will always try to connect to "test" database.

Meteor and Mongo in Windows 8

I started Meteor tutorial and I was able to run a few basic items said in the first 2 steps in the 3 step they ask to add this code
Tasks = new Mongo.Collection("tasks");
but when I try to run meteor in the app folder I got (tried it by running Meteor alone and Meteor + Meteor mongo in separate shell of same folder)
ReferenceError: Mongo is not defined
What am I missing here how to make the connection working ?
haha my mistake I think the tutorial given
https://www.meteor.com/try/3
is old or something the issue is sorted when you use
Tasks = new Meteor.Collecion
instead of
new Mongo.Collection

can't make basic mongo shell script with authentication

I have a really complicated issue that i think i can solve by writing a mongo shell script but i can't even manage to make a simple connection. I have a local mongo database which is requires a username/password that i normally access like this:
mongo admin -u <username> -p
at which point I enter the password and hooray! i have a shell. but that won't work for my issue. As a test, I created a file called test.js and all it has in it is this:
var conn = new Mongo()
db = conn.getDB("test");
db.cust.find();
I then run the script from the command line like so:
mongo test.js
at which point i get this:
MongoDB shell version: 2.4.10
connecting to: test
Why am i getting no results?
I finally made this work. This is how i ended up doing it:
First I made a file called test.js with the following in it:
db = connect("localhost:27017/admin");
db.auth('username','password');
db = db.getSiblingDB('test');
var cursor = db.cust.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}
I then ran this command from the command line:
mongo test.js
I also want to point out a few things that i learned while trying to do this to any other developer who is having issues.
1) if you add a new database, and your are running mongo with authentication you either need to log into the authentication database first and then switch to the desired database (as my example shows) or you need to add a user/password to the desired database (as i probably should have done in the first place)
2) When running a javascript file via mongo, don't expect to use the same "javascript" functions that you are used to. I just learned a hard lesson that not all javascript is the same. for example, you can not use Console.log() in a javascript file that is run via mongo because console.log is not actually core javascript but rather a function specific to browser and node implementations.

Meteor without mongo

With 0.6.5 release it is possible to develop non web apps with meteor.
I rebuild it from scratch for ARM processor but I don't want DB support at all. (Mongo is a processor killer, has to high footprint and I simply don't need it)
ARM should work as DDP client only, with this in mind I build it manually without mongo.
And tried to build simplest app possible
only 1 package at start (all standard packages removed)
meteor
and one file in server folder
main = function(argv){
return "DAEMON"
}
Meteor.setInterval(function(){
console.log("HellOnWorld");
},1000);
On machine with full meteor install it works as expected
but without mongo installed I got errors
Unexpected mongo exit code 127. Restarting.
Unexpected mongo exit code 127. Restarting.
Initializing mongo database... this may take a moment.
Unexpected mongo exit code 127. Restarting.
Can't start mongod
Obviously I don't have and want mongo.
Is there any way to start meteor without waiting for mongo db ?
Meteor team plans to support other db's so it must be implemented sooner or later.
UPDATE
For newer versions of Meteor you need to remove the mongo package. The mongo package is embedded in the meteor-platform package. So you need to remove that and add all the rest back (from https://github.com/meteor/meteor/tree/devel/packages/meteor-platform):
meteor remove meteor-platform
meteor add meteor webapp logging tracker session ddp blaze spacebars templating check underscore jquery random ejson templating check underscore jquery random ejson
Then your app won't use Mongo anymore :).
In dev mode you can get rid of mongo by setting the MONGO_URL environment variable to something else and start meteor. For example: MONGO_URL=mongodb://nowhere meteor
Turns out that if you just set any MONGO_URL environment variable before running meteor, it won't start its local MongoDB! Fantastic for testing packages that don't depend on Mongo.
Before:
$ meteor test-packages ./
Testing fortawesome:fontawesome-compat...
[[[[[ Tests ]]]]]
=> Started proxy.
=> Started MongoDB.
=> Started your app.
=> App running at: http://localhost:3000/
After
$ MONGO_URL=mongodb://mysql.com meteor test-packages ./ # haha
Testing fortawesome:fontawesome-compat...
[[[[[ Tests ]]]]]
=> Started proxy.
=> Started your app.
=> App running at: http://localhost:3000/
Look ma, no Mongo!
I have confirmed that no mongo process is started, and no .meteor/local/db folder is created.
In Meteor 0.6.5, you can embed TingoDb, a Node.js implementation of the MongoDB API, with your Meteor bundle instead:
1) Go to the programs/server directory in your bundle and do npm install tingodb to add TingoDb to your bundle.
2) Near the top of programs/server/packages/mongo-livedata.js, with all of the other Npm.require statements, add the following line
var Db = Npm.require('tingodb')().Db;
3) In that same file (programs/server/packages/mongo-livedata.js) replace the following code block
MongoDB.connect(url, options, function(err, db) {
if (err)
throw err;
self.db = db;
Fiber(function () {
// drain queue of pending callbacks
_.each(self._connectCallbacks, function (c) {
c(db);
});
}).run();
});
with this code:
var db = new Db('path/to/your/db/directory', {});
self.db = db;
Fiber(function () {
_.each(self._connectCallbacks, function (c) {
c(db);
});
}).run();
The path/to/your/db/directory can be anywhere, but is relative to the programs/server directory in your bundle by default.
4) To run your Meteor bundle, it wants you to export an environment variable called MONGO_URL. You could dive in to the code and remove the checks for this, but since it's never used you can just as easily export a fake MONGO_URL, like the one in your bundle's README file:
export MONGO_URL='mongodb://user:password#host:port/databasename'
5) From your bundle's base directory run node main.js.
Caveat emptor: obviously you're messing around with Meteor internals here, and this will almost assuredly break with future versions.
Meteor 1.2.1 - Just set
MONGO_URL=none
for an environment variable. (none isn't a keyword, anything invalid appears to prevent mongo from starting)
As a weird possibility, make a mock mongo server on the right port, and set your environnmental variable to access it. I'd bet you only need a few handshake routines be implemented and no more traffic after that. Beyond my capability, but it does have the advantage of not needing to adopt to code changes as things shift.
The listed answers are not working with the Meteor 1.x.
Following is my way to run meteor without mongodb and doesn't need modify anything(neither source code nor packages configuration) in meteor.
git clone https://github.com/solderzzc/mongodb-fs
cd mongodb-fs && npm install && node samples/test-server.js
you will see following console log if everything goes well
meteor create --example leaderboard && cd leaderboard
MONGO_URL=mongodb://localhost:27027/fakedb meteor
Add point to the player, and check with the mongo command line:
mongo localhost:27027/fakedb

When using a separate MongoDB with meteor, meteor reset stopped working

I have my MONGO_URL set to mongodb://localhost:27017/meteor and have the MongoDB run as a service.
When running my project it seems OK to store data to the separate MongoDB until I tried to run meteor reset.
My assumption is it tried to remove its default database. The error complained that myproject.meteor\local is not empty and pointed to fs.js:456 which goes to files.js:256 (rm_recursive) and so on.
any idea what and how I can fix this?
$ meteor reset only resets the bundled MongoDB. It won't reset an external Mongo database.
(That's something we should explain better in the documentation.)
In your case, try connecting to the Mongo database directly (with the mongo command line shell) and running > db.dropDatabase()