A Repository created to describe the usage of pymongo a Python-MongoDB driver to connect, insert, select, update and delete Documents with MongoDB database.
Install pymongo using pip
pip install pymongo
Or visit Here For Manual Download
- Python 3.11
- MongoDB 4.0.4 - Database Server
- (Optional)MongoDB Compass 1.16.3 - To view database
Import the package
import pymongo
The first step when working with PyMongo is to create a MongoClient to the running mongod instance. Doing so is easy:
from pymongo import MongoClient
client = MongoClient()
The above code will connect on the default host and port. We can also specify the host and port explicitly, as follows:
client = MongoClient('localhost', 27017)
Or use the MongoDB URI format:
client = MongoClient('mongodb://localhost:27017/')
A single instance of MongoDB can support multiple independent databases. When working with PyMongo you access databases using attribute style access on MongoClient instances:
db = client.test_database
If your database name is such that using attribute style access won’t work (like test-database), you can use dictionary style access instead:
db = client['test-database']
A collection is a group of documents stored in MongoDB, and can be thought of as roughly the equivalent of a table in a relational database. Getting a collection in PyMongo works the same as getting a database:
collection = db.test_collection
or (using dictionary style access):
collection = db['test-collection']
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.
MongoDB Atlas is a fully-managed cloud database that handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS , Azure, and GCP). MongoDB Atlas is the best way to deploy, run, and scale MongoDB in the cloud.
Access the file here to get started with MongoDB atlas Here
- Connect with Local
- Connect with Atlas
- Insert
- Select
- Update
- Delete
- DropCollection
- Modifiers
- Sort
- Limit
- MetaData
If you have any feedback, please reach out to us at voidex.developer@gmail.com