GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.geeksforgeeks.org — MongoDB and Python - GeeksforGeeks MongoDB and PythonLast Updated : 19 Sep, 2026 MongoDB is an open-source NoSQL database that stores data in flexible, JSON-like documents (BSON format).
MongoDB vs RDBMS![]() Prerequisites
Install pymongo using following command:
Connecting MongoDB with Python Using PyMongoStep 1: Start MongoDBStart MongoDB from the command prompt using the following command: If starting with default settings:
When running directly with custom data directory:
If MongoDB is installed as a Windows service:
![]() See port number by default is set 27017 (last 2-3 lines in above image). Step 2: Install and Import PyMongoPyMongo is the native Python library for MongoDB. To install PyMongo, open Command Prompt and run:
After installation, open a Python environment and import the library:
Step 3: Connect to MongoDB Server LocallyTo connect to a local MongoDB server, create a MongoClient object:
Connect to the default MongoDB host and port (localhost:27017) using the following command to create a MongoClient explicitly.
Step 4: Connect to MongoDB Atlas (Cloud Database)To connect to a cloud-based MongoDB database like MongoDB Atlas, follow these steps: 1. Create a MongoDB Atlas Account: Go to MongoDB Atlas and create an account if you don't already have one. Once logged in, create a new project, cluster, and database. 2. Get the Connection String: After creating the cluster, navigate to the Clusters tab, click Connect, and choose Connect your application. Select Python and copy the provided connection string. 3. Connection string typically looks like this:
4. Use the Connection String: Replace <username> and <password> with your MongoDB Atlas credentials.
This will connect your Python application to the cloud-based MongoDB cluster. Step 5: Access/Create a DatabaseTo create a database or switch to an existing database use:
Or:
Step 6: Access/Create a CollectionCollections are conceptually similar to tables in Relational Database Management Systems (RDBMS), but they store flexible JSON-like documents instead of rows and columns. To access the collection named "myTable" from the "my_database" database:
MongoDB stores data as BSON (Binary JSON) documents, which are represented as Python dictionaries when working with PyMongo.
'_id' is a 12 bytes hexadecimal number. A special key which uniquely identifies each document in a collection and automatically added when not added explicitly. ![]() Step 7: Inserting data inside collectionMethods used:
Insert the document into a collection:
The complete code looks like this when implemented:
Step 8: Querying the DatabaseCertain query functions are used to filter data in a MongoDB database. Among them, the two most commonly used are: find(): used to retrieve multiple documents from a collection that match a given query.
count_documents(): used to count the number of documents in a collection that match a specific query.
To print all the documents/entries inside 'myTable' of database 'my_database':
Explanation:
PyMongo Methods for CRUD
Comment Explore | ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |