Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I get list all MongoDB databases and collections for individual databases using python only(Without using any of mongo API)

Any help would be appreciated.

share|improve this question
 
Shell out to mongo CLI. –  Sergio Tulentsev Oct 28 '13 at 10:14
 
Why would you care if it's Python if you don't want to use any existing APIs? The solution would require that the MongoDb console would be available on the clients of the database. –  WiredPrairie Oct 28 '13 at 10:40
 
@WiredPrairie: alternatively, OP can open a socket and start writing binary data to it, which will be recognised as valid commands by mongodb server. :) –  Sergio Tulentsev Oct 28 '13 at 10:44
 
@SergioTulentsev - sounds good. Maybe edit your answer to include that instead of the console. ;) –  WiredPrairie Oct 28 '13 at 10:50
 
@captain: that was a joke, actually. Just use the python mongo driver. –  Sergio Tulentsev Oct 28 '13 at 11:56
show 5 more comments

closed as off-topic by WiredPrairie, Sergio Tulentsev, Mark, nmaier, ithcy Oct 28 '13 at 16:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Sergio Tulentsev, Mark, nmaier, ithcy
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

Here's a JS command that will get database names

db.getMongo().getDBNames().forEach(function(db) {
  print(db);
});

Put it to a file, say "dbs.js" and run mongo command line tool with the file as argument:

mongo dbs.js

Output:

% mongo dbs.js
MongoDB shell version: 2.4.6
connecting to: test
mydb_development
mydb_test
local

How to call command line tool ("shelling out") and read tool's output - it's easily googlable stuff. That'll be your homework.

share|improve this answer
add comment

I'm using Pymongo

db = MongoClient('mongodb://127.0.0.0:123456'['my_db']
for c in db.collection_names():
    print c


system.indexes
active_cashier_orders
order_data
notification_profiles
notifications
recos_orders
profile_discounts
email
task_locks
backoffice_settings
active_cashier_status
pos_manager
sessions
shop_eta
last_check_time
srfax
fax_provider

share|improve this answer
 
I dont wanna go for pymongo module.any other thought? –  Pilot Oct 28 '13 at 12:15
1  
Besides, OP wants to list databases, not collections –  Sergio Tulentsev Oct 28 '13 at 12:43
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.