Objective and Prerequisites

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
kodingwindow@kw:~$ sudo systemctl start mongod
kodingwindow@kw:~$ 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.
kw.py
import pymongo
from pymongo import MongoClient
client=MongoClient()
print("Connection Established")

client=MongoClient('localhost',27017)
db=client.DB
collection=db.holders

holder1={
'account_no': 2562348989,
'name': 'James Moore',
'bank': 'Barclays',
'amount': 5000
}

holder2={
'account_no': 2562348990,
'name': 'Donald Taylor',
'bank': 'Citi',
'amount': 7000
}

holder3={
'account_no': 2562348991,
'name': 'Edward Parkar',
'bank': 'ICICI',
'amount': 95000
}

docs=db.holders.insert_many([holder1,holder2,holder3])
print("Documents inserted successfully")

for holder in db.holders.find():
    print(holder,"\n")

# Disabled _id attribute
for holder in db.holders.find({},{'_id': 0,'account_no': 1,'name':1}):
    print(holder)
Output
kodingwindow@kw:~$ python3 kw.py
Connection Established
Documents inserted successfully
{'_id': ObjectId('662dff2171fef7fb94353a22'), 'account_no': 2562348989, 'name': 'James Moore', 'bank': 'Barclays', 'amount': 5000} 

{'_id': ObjectId('662dff2171fef7fb94353a23'), 'account_no': 2562348990, 'name': 'Donald Taylor', 'bank': 'Citi', 'amount': 7000} 

{'_id': ObjectId('662dff2171fef7fb94353a24'), 'account_no': 2562348991, 'name': 'Edward Parkar', 'bank': 'ICICI', 'amount': 95000} 

{'account_no': 2562348989, 'name': 'James Moore'}
{'account_no': 2562348990, 'name': 'Donald Taylor'}
{'account_no': 2562348991, 'name': 'Edward Parkar'}
MongoDB Instance
test> show dbs
DB      0.000GB
admin   0.000GB
config  0.000GB
local   0.000GB

test> use DB
switched to db DB

DB> db.getCollectionNames()
[ "holders" ]

DB> db.holders.find().pretty()
[
  {
    _id: ObjectId('662dff2171fef7fb94353a22'),
    account_no: Long('2562348989'),
    name: 'James Moore',
    bank: 'Barclays',
    amount: 5000
  },
  {
    _id: ObjectId('662dff2171fef7fb94353a23'),
    account_no: Long('2562348990'),
    name: 'Donald Taylor',
    bank: 'Citi',
    amount: 7000
  },
  {
    _id: ObjectId('662dff2171fef7fb94353a24'),
    account_no: Long('2562348991'),
    name: 'Edward Parkar',
    bank: 'ICICI',
    amount: 95000
  }
]
Advertisement