Steps to create a new Mongo database and collection


There aren’t any. Really.

Like many things in NoSQL, you just “use it as if it exists, and then it magically does exist.” Try typing some variant of this into the Python shell:

>>> import pymongo
>>> mc = pymongo.MongoClient("mongodb://localhost:27017")
>>> db = mc['randomName1']
>>> db.randomName2.insert_one({'someKey':'someValue'})

You will discover if you then log in to the mongosh that there is in fact a database now called randomName1, and in it is a collection called randomName2, which has one document, with an _id and a "someKey":"someValue" key-value pair.

So you never had to prepare any of the above Python code to work by issuing a command to create a new database, or a new collection. Scary, but kinda neat!