Basically this is a list in progress of common errors/tasks/gripes I get when using Mongo. I’ve noted down what usually works. Maybe you’ll find it useful 🙂
Why has my remote mongodb connection been refused?
1 2 3 4 |
from pymongo import MongoClient c = MongoClient('some ip address') ... pymongo.errors.ConnectionFailure: could not connect to 123.456.0.1:27017: [Errno 111] Connection refused |
- Delete the mongod.lock file from your main mongodb storage folder and restart [SO]
- If on redhat, check sestatus
- Modify /etc/sysconfig/iptables to have the correct firewall rules according to the mongodb docs.
How do can you iterate through all mongoDB collections in pymongo?
I normally access collections as object attributes like: conn.Database.Collection.find_one() but actually databases and connections can be accessed as keys in a dictionary as well:
1 2 3 4 5 6 7 8 9 10 11 |
from pymongo import MongoClient conn = MongoClient() #use this instead of Connection() #[1:] is used here because the first collection is system.indexes for collection in conn.Database.collection_names()[1:]: #Do something conn.Database[collection].find_one() #We can say anything like: conn['Database'].collection.find_one() conn.Database['collection'].find_one() |
Why is mongod terminating whenever I close the shell? Even when using & at the end
When starting mongod, use mongod --fork (note: fork must be right after the word mongod) and it will start as a background process instead. Or just add fork = true to your config.
I just created an authenticated database and can’t even use show dbs !
Create a new user with all 4 of the following permissions: userAdminAnyDatabase, readWriteAnyDatabase, dbAdminAnyDatabase, clusterAdmin:
1 2 3 4 5 6 7 8 9 10 11 12 |
> db.addUser({ user: "hilarious_name", pwd: "something_memorable", roles: ['clusterAdmin','userAdminAnyDatabase', 'dbAdminAnyDatabase' 'readWriteAnyDatabase'] } ) { "user" : "hilarious_name", "pwd" : "0c3032130d7123a36c1913b2f55a3c84", "roles" : [ "clusterAdmin", "userAdminAnyDatabase", "readWriteAnyDatabase", "dbAdminAnyDatabase", ], "_id" : ObjectId("535956f010b65cad1f7906f7") } |