MongoDB w/ PyMongo on Heroku -- error: no module named pymongo - mongodb

I am making a heroku app using Flask with a mongoDB backend. I am using pymongo as my driver but when I push my code to git, it crashes and spits out this error:
2014-04-05T09:56:36.301695+00:00 app[web.1]: ImportError: No module named pymongo
The web-app works locally and pymongo works completely fine in that scenario. What do I have to do to have pymongo recognized on heroku's servers?
Thansks!

You need to make Heroku aware of PyMongo as a requirement. The easiest way to do this is my adding pymongo==x.x to your requirements.txt file.
If you are using a setup.py script instead of requirements.txt, add 'pymong==x.x', to the install_requires argument to setup.
In both cases, make sure to replace x.x with the appropriate version number.

Related

Using psycopg2 directly on Google AppEngine

When using Google Appengine Flexible + Python, how to use Psycopg2 directly (without SQLAlchemy) to access a CloudSQL PostgreSQL database?
Hello myselfhimself,
Here is a solution:
in your app.yaml, add an environment variable, imitating Google Appengine Flexible Python CloudSQL documentation's SQLAlchemy's URI but without the psycopg2+ prefix:
env_variables:
PSYCOPG2_POSTGRESQL_URI: postgresql://user:password#/databasename?host=/cloudsql/project-name:region:database-instance-name
in any python file to be deployed and run, pass that environment variable to psycopg2's connect statement directly. This leverages psycopg2.connect's ability to pass the URI directly to the psql client library (this might not work with older PostgreSQL versions..).
import os
import psycopg2
conn = psycopg2.connect(os.environ['PSYCOPG2_POSTGRESQL_URI'])
when working locally with the google cloud proxy tool, make sure you set the URI environment variable first, if your local server is not aware of app.yaml:
export PSYCOPG2_POSTGRESQL_URI="postgresql://user:password#/databasename?host=/cloudsql/project-name:region:database-instance-name"
./cloud_sql_proxy -instances=project-name:region:database-instance-name=tcp:5432
#somewhat later:
python myserver.py
I hope it will work for your too : )

How to copy database from MongoDB 1.6 instance

I have a MongoDB instance with two databases, let's call them "realdb" and "copydb".
All I want to do is to periodically copy realdb to copydb. The copydb database is our "testing" instance of the actual database "realdb", and we want to periodically update it.
Normally the answer to this question would be "copydb" or "export/import". However, there are some challenges:
The target Mongo instance is running version 1.6 and is not likely to be updated anytime soon.
The target Mongo instance is on a remote server to which I don't have direct access.
This seems like basic enough a function that even 1.6 should have the ability to do it. But when I try anything I get "No such cmd" errors as if the newer Mongo can't communicate with the ancient Mongo.
Any thoughts on how this could be done?
The error "no such command" might mean that:
The command was mistyped, or does not exist
There is a mismatch of the client version (e.g. mongo shell, mongodump, mongoexport, or a mongodb driver) and the server version.
In this case it was a mismatch of the mongo shell version (3.4) vs server version (1.6).

Mongodb "auth fails" with mongodb php driver and new php library

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.
So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

ReferenceError: require is not defined in MongoDB shell

I try to connect MongoDB from Mongo client on windows command (Window 8.1). When I use require() in javascript, I have error as below. Does any one has same issue? Did I miss any require related npm installation? How can't MongoDB shell find require function?
C:\tutorial\nodeMongoAngular-master\lesson2>mongo
MongoDB shell version: 3.0.1
connecting to: test
var MongoClient = require('mongodb').MongoClient;
2015-04-30T14:33:25.812-0400 E QUERY ReferenceError: require is not defined
at (shell):1:19
You are confusing the mongo administrative shell with the Node.js driver. While both environments happen to use JavaScript, the mongo shell has more limited I/O support and is not intended to be used as a driver for application development.
If you want to write Node.js applications using the MongoDB driver (as per your example code), you need to use the node interpreter. The Node.js driver documentation includes a Quickstart tutorial with examples that should help you get started.
#Scott Lee: if you're still looking for an answer, try running with command "node yourscript.js". Make sure mongod is running. 'mongo script.js' will work for pure mongo scripts without nodejs code.

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