Using "meteor mongo" on localhost but with remote Database - mongodb

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'
}
});
}
});

Related

How to check meteor production database [duplicate]

It seems the answer in this thread (Accessing Meteor production database) does not work anymore when you want to access a meteor production database in 2016. I want to access a meteor production database blah.meteor.com using
meteor mongo blah.meteor.com
instead what I get is:
connecting to: sg-mother1-6243.servers.mongodirector.com:27017/blah_meteor_com
2016-01-18T15:21:49.884+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
Then I tried
meteor mongo --url blah.meteor.com
I get username cursor. I enter my meteor site username and press enter and then get password cursor. I enter password for the above username and press enter. I get presented with the following url:
mongodb://client-2ee8c14d:c1546ca8-4e7e-5883-0214-150b309fb4fb#SG-mother1-6242.servers.mongodirector.com:27017/blah_meteor_com
Then every time I re-enter
meteor mongo --url blah.meteor.com
I am assumed to have logged on already, and I just get presented with a similar url to the one I was presented with just above.
I read the "meteor mongo command" documentation by entering:
meteor mongo --help
In the documentation I read the following line:
Instead of opening a shell, specifying --url (-U) will return a URL
suitable for an external program to connect to the database. For remote
databases on deployed applications, the URL is valid for one minute.
For the meaning, I went back to the thread (stackoverflow.com/questions/11801278/accessing-meteor-production-database) I mentioned in the beggining and read:
"So what it's saying is, the url provided by running the command with the --url option is for connecting to the database by some external application, i.e. other than meteor."
I don't know what other application can help me connect to meteor production database other than what I used to do in 2015, which is:
meteor mongo blah.meteor.com
I read somewhere that I can use the mongo shell intead but I don't know how to open it and I don't know the mongo installation directory when it is installed with meteor. I am using linux (fedora) OS.
How do I access meteor production database in 2016? Are there upgrades that happened that make me not to be able to access meteor production database as easily as I did in 2015?
You are trying to connect to a database version 3.0 while your meteor mongo command still use the version 2.6.7 of mongo
Try this workaround :
Install Mongo version (3.x) directly on your machine.
Then run this command (should work on osx, linux and windows when sed is installed):
mongo `meteor mongo --url XXX.meteor.com | sed 's/mongodb:\/\//-u /' | sed 's/:/ -p /' | sed 's/#/ /'`
Source: https://forums.meteor.com/t/meteor-mongo-xxx-meteor-com-giving-exception-login-failed-workaround/15289
Since Meteor stopped supporting the use of .meteor domains and every developer needs to get his hosting by himself, I found a way accessing the remote's database by using mup or mupx. I wrote it in this post: https://stackoverflow.com/a/37439315/2908071
I hope this will help future people.

Cannot connect to alternate Mongo DB in Meteor App

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)

how can I connect to a remote mongo server from Mac OS terminal

I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI:
mongodb://username:password#somewhere.mongolayer.com:10011/my_database
I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program in order to connect to this remote database.
However, I can't find the right command to get me the full shell access I would like. Using instructions found here http://docs.mongodb.org/manual/reference/program/mongo/ (search for "remote") I am able to get what looks like a connection, but without giving my username or password I am not fully connected. Running db.auth(username, password) returns 1 (as opposed to "auth fails" when I provide incorrect username and password), but I continue to get an "unauthorized" error message when issuing the show dbs command.
You are probably connecting fine but don't have sufficient privileges to run show dbs.
You don't need to run the db.auth if you pass the auth in the command line:
mongo somewhere.mongolayer.com:10011/my_database -u username -p password
Once you connect are you able to see collections?
> show collections
If so all is well and you just don't have admin privileges to the database and can't run the show dbs
With Mongo 3.2 and higher just use your connection string as is:
mongo mongodb://username:password#somewhere.mongolayer.com:10011/my_database
Another way to do this is:
mongo mongodb://mongoDbIPorDomain:port

Connect to Mongo from Meteor

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");

Mongo db database not accessible from shell, data storage

I am working with deployd which uses mongoDB as a db on Ubuntu OS.
All databases of mongoDB are accessible from within mongodb shell but my question is when I create a new project with deployd I can store/read/modify records but this new database is not accessible from mongodb shell, how do i do that?.
Database files for this new project are stored within the application directory not in mongoDB default data directory.
Also what's the difference of mongoDB as a database within application and MongoDB as a global database for all applications.
Currently all collections are accessible using dpd object like
dpd.albums.get(inputParams, function(resultSet, err){ //handle result});
While I would like to access these collections using something like
db.albums.find(); //within mongod shell
It seems there are a few misconceptions here about the deploying with "deployd".
Firstly "deployd" depends on an existing mongoDB installation on it's deployed host or is otherwise configured to use a host on another server. So where "data files" are kept are generally a point of condifuration. All that "deployd" tries to do is start the "mongod" instance as it's own instance is started should the configuration be local.
No matter what you do, all of the options are available in configuration. There is a good sample in the documentation here:
var server = deployd({
port: process.env.PORT || 5000,
env: 'staging',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
As you can see the host and port settings are specified in the configuration of the application itself, so where you are not using the default "mongoDB" host and port then add them to the command line options, as in:
mongo --host my.production.mongo.host --port 27105
So even on the same localhost, note that the instance does not use the default 27017 port but a different one that you would need to specify when connecting.
Also see the mongo shell command documentation as well as the deployd documentation.