PyMongo - Create database with no collections or documents

Hello,

I’m creating some functions with Python using PyMongo, create database and create collection. I’ve read the docs and it says that in order to databases to be created (committed) a document must be written.

Is there a way to only create a database without having collections and documents? Or is there a way to handle this kind of situation?

Short answer no… MongoDB doesn’t provide any command to create “database”

if you drop all collections in a database, you delete the database too.

you may try yourself;

List all databases.

show dbs

switch the database you want to create (non-exists database name will work)

use new_database;

Insert an empty document into the collection you want to create (again, non-exists collection name will work)

db.new_collection.insertOne({});

drop the collection, you just created and inserted a document.

db.new_collection.drop();

List all databases, again.

show dbs

Why do you want/need to create an empty database? It doesn’t feel very MongoDB-ish…