Python MongoDB Create Database

In MongoDB, databases are not explicitly created by a command. Instead, a database is created when you insert data into a collection in that database for the first time. This means that as soon as you store a document in a collection, MongoDB will automatically create the database and the collection if they don’t exist.

Here’s how to create a database in MongoDB using Python and pymongo:

Steps:

  1. Install the PyMongo library (if not already installed):
    pip install pymongo
    
  2. Connect to MongoDB, create the database, and insert data.

Example: Creating a Database in MongoDB Using Python

import pymongo

# Step 1: Connect to MongoDB (change the URL as needed)
client = pymongo.MongoClient("mongodb://localhost:27017/")

# Step 2: Create a database (it will be created when you insert data)
mydb = client["mydatabase"]

# Step 3: Create a collection (similar to a table in SQL)
mycol = mydb["customers"]

# Step 4: Insert a document (row) into the collection
mydict = { "name": "John", "address": "123 Elm Street" }

x = mycol.insert_one(mydict)

# Step 5: Print the databases to verify creation
print(client.list_database_names())  # mydatabase will appear only after inserting data

Explanation:

  1. Connecting to MongoDB: MongoClient("mongodb://localhost:27017/") connects to the local MongoDB server. You can replace localhost with the IP or URL of a remote MongoDB server.
  2. Create a Database: The line client["mydatabase"] doesn’t immediately create the database. MongoDB creates the database only when data is inserted into it.
  3. Create a Collection: The line mydb["customers"] references the customers collection. Again, this collection isn’t created until you insert a document.
  4. Insert a Document: Using insert_one(), we insert a document (like a row in a SQL table). This action triggers the creation of both the mydatabase and the customers collection.
  5. Verify Creation: After inserting data, the database will now exist, and you can check all databases by calling client.list_database_names().

Listing Existing Databases

Once you insert the first document, you can list all databases in the MongoDB instance:

# List all databases
print(client.list_database_names())

This will output a list of all databases, including the newly created one (e.g., mydatabase).

Important Points:

  • MongoDB does not create databases until data is inserted.
  • The same is true for collections: they only exist once documents are inserted into them.
  • If you don’t insert any documents, the database won’t appear in the database list.

Example with Multiple Collections:

If you want to create more collections in the same database:

# Create another collection (e.g., 'orders')
orders_col = mydb["orders"]

# Insert a document into the 'orders' collection
order_dict = { "product": "Laptop", "quantity": 1 }
orders_col.insert_one(order_dict)

# Now both 'customers' and 'orders' collections exist in 'mydatabase'
print(mydb.list_collection_names())

This will show the collections within mydatabase, such as customers and orders.

In summary, databases and collections in MongoDB are created on the fly when you first insert data.

Leave a Reply 0

Your email address will not be published. Required fields are marked *