I want to create a database and collection using python script so that I used pymongo driver. But I am unable to create database.

Steps I followed:

from pymongo import MongoClient
con = MongoClient('localhost', 27017)
db = con.testdb

After this commands when I checked in mongoshell the db "testdb" is not created.

Can I use paramiko to interact with mongo shell ?

python version : 2.7

  • MongoDB creates new databases implicitly upon their first use, so you need to insert at least one document into a collection in testdb in order to see the database created.
    – Stennie
    Apr 1 '18 at 13:19

From http://api.mongodb.com/python/current/tutorial.html

It states

An important note about collections (and databases) in MongoDB is that 
they are created lazily - none of the above commands have actually 
performed any operations on the MongoDB server. **Collections and 
databases are created when the first document is inserted into them.**

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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