How do I access Meteor's MongoDB from another client, while Meteor is running?

PythonMongodbMeteor

Python Problem Overview


I would like to access Meteor's MongoDB from a Python client, while Meteor is running.

I can't start a mongod because Meteor's database is locked.

How do I access the database from another client?

Python Solutions


Solution 1 - Python

The meteor command provides a clean way. To get the URL for the running mongod:

meteor mongo -U

which you can parse from python.

Solution 2 - Python

Meteor starts the mongod for you on port 3002 when you run the meteor command, and stores the mongo data file in .meteor/local/db

Output from ps aux | grep 'mongod' shows the mongod command that meteor uses:

/usr/local/meteor/mongodb/bin/mongod --bind_ip 127.0.0.1 --smallfiles --port 3002 --dbpath /path/to/your/project/.meteor/local/db

So just connect your mongo client accordingly. In python:

>>> import pymongo
>>> con = pymongo.Connection(host='127.0.0.1', port=3002)
>>> con.database_names()
[u'meteor', u'local']

UPDATE: unfortunately making changes directly in mongo in this way won't reflect live in the app, but the changes will be reflected on a full page (re)load.

Solution 3 - Python

Use the Meteor deployment instructions

The command will look like this:

   PORT=3000 MONGO_URL=mongodb://localhost:27017/myapp node bundle/main.js

Solution 4 - Python

You can also find it from within server side code using:

process.env.MONGO_URL

Even if you don't set this environment variable when running, it gets set to the default. This seems to be how it is found internally (packages/mongo/remote_collection_driver.js)

The one is given by meteor mongo -U seems to reconstruct the default domain/ip and db-name, but use the port stored in the file.

You can put this anywhere in the server folder, and read it from the command line.

console.log('db url: ' + process.env.MONGO_URL);

I set up a webpage to display it to double check in the selenium tests that we are using the test database, and not overwriting live data.

Solution 5 - Python

And here a shell script to get Mongo URI and Mongo Database:

#!/bin/bash -eux

read -s -p "Enter Password: " password

cmd=$(meteor mongo --url myapp.meteor.com << ENDPASS
$password
ENDPASS)
mongo_uri=$(echo $cmd | cut -f2 -d" ")
mongo_db=$(echo $mongo_uri | cut -d/ -f 4)

#my_client_command_with MONGODB_URI=$mongo_uri MONGO_DB=$mongo_db

Solution 6 - Python

In regards to a 10 second delay on updates: Tail the MongoDB oplog! There's more information on how to do it here:

http://meteorhacks.com/lets-scale-meteor.html

Make sure you install smart collections and use those (instantiate your collections using Meteor.SmartCollection instead of Meteor.Collection) and you will find the updates are essentially immediate.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoseph TurianView Question on Stackoverflow
Solution 1 - PythondebergalisView Answer on Stackoverflow
Solution 2 - PythondannyView Answer on Stackoverflow
Solution 3 - PythonJoseph TurianView Answer on Stackoverflow
Solution 4 - PythonAlex028502View Answer on Stackoverflow
Solution 5 - PythonPierre OzouxView Answer on Stackoverflow
Solution 6 - PythonAdam HoddinottView Answer on Stackoverflow