I want to connect the Mongo DB on the other server with meteor on my local machine.
Any help appreciated.I am new to meteor.
error on running meteor
Can't start Mongo server. MongoDB had an
unspecified uncaught exception. This can be caused by MongoDB being
unable to write to a local database. Check that you have permissions
to write to .meteor/local. MongoDB does not support filesystems like
NFS that do not allow file locking.
On the meteor app machine, on the server side use this piece of code.
if(Meteor.isServer){
Meteor.startup(function () {
var myDatabase = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: myDatabasee });
});
}
The only you need to know its the name of the url <mongo url> it could be something like mongodb://127.0.0.1:27017/local or meteor
Related
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.
I am running a Meteor app. locally using a Virtual Machine (VM). I want to use a new Mongo DB that I have set-up on the VM called 'my_db'. I have set the MONGO_URL on the command line like this:
export MONGO_URL='mongodb://127.0.0.1:3001/my-db'
I have checked that this environment variable is set using echo $MONGO_URL and I get back:
mongodb://127.0.0.1:3001/my-db
However, when I start up my Meteor app. it still connects to the default 'meteor' database. Why is this and what can I do to get it connecting to my alternative 'my_db' database?
Like #dr.dimitr say env vars are only used on the production process (for example connecting your modulus app with the modulus db they provide or another different), on the developing process you can use the mongo drive like this.
if(Meteor.isServer){
Meteor.startup(function () {
var myDatabase = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: myDatabase });
});
}
Now you need to know name of the url <mongo url> which could be something like mongodb://127.0.0.1:27017/local or meteor
Some Related Posts - (1) and (2)
I am trying to connect to my Mongo database which is situated on the machine as my Meteor app. Here are two files in my app:
a.js:
if (Meteor.isServer) {
var database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/meteor");
Boxes = new Mongo.Collection("boxes", { _driver: database });
Meteor.publish('boxes', function() {
return Boxes.find();
});
}
b.js:
if (Meteor.isClient) {
Meteor.subscribe('boxes');
Template.homeCanvasTpl.helpers({
boxes: function () {
return Boxes.find({});
}
});
}
But I keep getting a "Exception in template helper: ReferenceError: Boxes is not defined" error - any ideas?
How can you connect to a MongoDB with Meteor?
Scenario A: Use the built-in DB as default
This is much simpler than what you did. When you run meteor you actually start a DB with the Meteor server, where Meteor listens on port 3000 and the database on port 3001. The Meteor app is already connected to this database at port 3001 and uses a db named meteor. There is no need whatsoever to fall back to MongoInternals.RemoteCollectionDriver. Just remove that code and change things to:
Boxes = new Mongo.Collection("boxes"); // use default MongoDB connection
Scenario B: Use a different DB as default
Using the MONGO_URL environment variable you can set the connection string to a MongoDB when starting the Meteor server. Instead of the local port 3001 database or an unauthenticated connection you can specify exactly where and how to connect. Start your Meteor server like this:
$ MONGO_URL=mongodb://user:password#localhost:27017/meteor meteor
You can also leave out the user:password# part of the command if no authentication is needed.
Scenario C: Connect to a second (3rd, etc) DB from the same Meteor app
Now we need to use MongoInternals.RemoteCollectionDriver. If you wish to use another database that is not the default DB defined upon starting the Meteor server you should use your approach.
var database = new MongoInternals.RemoteCollectionDriver('mongodb://user:password#localhost:27017/meteor');
var numberOfDocs = database.open('boxes').find().count();
Bonus: Why should you not use MongoInternals with Mongo.Collection?
As the docs indicate you should not pass any Mongo connection to the new Mongo.Collection() command, but only a connection to another Meteor instance. That means, if you use DDP.connect to connect to a different server you can use your code - but you shouldn't mix the MongoInternals with Mongo.Collection - they don't work well together.
Based on feedback from saimeunt in the comments above, s/he pointed out that MongoInternals is unavailable to the client portion of a Meteor app. Therefore, the solution was to add in the line "Boxes = new Mongo.Collection("boxes");" to the client logic - here was the final working solution:
a.js:
if (Meteor.isServer) {
var database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/meteor");
Boxes = new Mongo.Collection("boxes", { _driver: database });
Meteor.publish('boxes', function() {
return Boxes.find();
});
}
b.js
if (Meteor.isClient) {
Boxes = new Mongo.Collection("boxes");
Meteor.subscribe('boxes');
Template.homeCanvasTpl.helpers({
boxes: function () {
return Boxes.find({});
}
});
}
Meteor has 2 different environment : the server environment running on Node.JS and the client environment running in browsers.
In your code you declare the Boxes Mongo collection only in the server environment, you need to take this declaration out of the Meteor.isServer condition (and BTW don't use these, separate your code in server/, client/ and lib/ directories).
Also, not sure if you need to connect to your MongoDB this way, maybe you should look into the MONGO_URL environment variable it probably already does what you need ? (provide a mongo connection URL to a distant (or local) Mongo server).
I have Windows PC running two ubuntu virtual machines. One has Meteor installed (app server), to other has Mongo installed (the purpose of this is to use Mongo away from the app server so that it will scale later on). Problem is that I cannot connect to Mongo instance from Meteor!
I can connect to the mongo instance from the the app server when using the line below on the command line and can retrieve data from the collection:
mongo 192.168.56.112/mydb -u myusername -pmypassword
I can also start my meteor app. using:
MONGO_URL="mongodb://myusername:mypassword#192.168.56.112/mydb" meteor
However, when doing the latter, I get an error "ReferenceError: mycollection is not defined".
Can anyone help my to identify why meteor won't connect to mongo?
As per Evgeny's suggestion, the answer is to define a variable for my collection:
Chatrooms = new Meteor.Collection("chatrooms");
I'm following the telescope tutorial.
I created a /client/collections/myfile.js
I'm on localhost, but I'm launching Meteor with remote DB hosted on MongoHQ instead of using Meteor's local DB.
In this tutorial I'm told to insert a new post by opening the Mongo console.
$ meteor mongo
How can I:
$ meteor mongo (somehow connect to my remote DB to use the meteor commands in terminal
So that I can:
$ db.collectionname.insert({ stuff });
Or does this have nothing to do with "Meteor" in this case and I just use a Mongo shell outside of Meteor? The collection that I created in "/client/collections/collection.js" is this simply for telling Meteor which collection to push as a subset to the client?
I'd like to use the same DB ( remotely hosted with MongoHQ) for my localhost development, and my actual live dev.mysite.com so when I deploy to this dev site, anything I've done in the DB is also there and ready to go.
Assuming you had a username of username, a password of PASSWORD, a database named test, and a hostname of hatch.mongohq.com:
Connecting via the shell
$ mongo hatch.mongohq.com:27017/test -u username -p PASSWORD
Connecting via Meteor
$ MONGO_URL="mongodb://username:PASSWORD#hatch.mongohq.com:27017/test" meteor
Other notes
You should define your Meteor collections outside of the client directory so they can be used on both the client and the server. See this for more details.
You will find that connecting to a remote database is much slower than connecting locally, so it's generally not recommended for development.
Meteor creates a dev database for you when it starts. This also affords you the very helpful commands: meteor reset and meteor mongo, to reset, and connect to said database.
Initializing your database
Create a file on the server for initialization - e.g. server/initialize.js. When the server starts you can add users or other documents which do not yet exist. For example:
Meteor.startup(function() {
if (Meteor.users.find().count() === 0) {
Accounts.createUser({
username: 'jsmith',
password: 'password',
profile: {
firstName: 'John',
lastName: 'Smith'
}
});
}
});