Objective: This segment introduces how to insert and retrieve the documents from a collection in MongoDB using Python.
Prerequisites: PyMongo is installed, and MongoDB is up and running in the background. If not, then refer to the Python Database Connectivity segment in sequence.
Start MongoDB
godarda@gd:~$ sudo systemctl start mongod
godarda@gd:~$ mongosh
Current Mongosh Log ID: 65154469633b7d0fc9cad531
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB: 7.0.9
Using Mongosh: 2.2.5
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
...
test> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
Python MongoDB to insert and retrieve the documents from a collection
In the following program, database and collection automatically get created. If database and collection already exist in MongoDB, then new documents will get inserted into an existing collection.
Dear User, Thank you for visitng GoDarda. If you are interested in technical
articles, latest technologies, and our journey further, please follow us on LinkedIn.