Skip to content
Related Articles

Related Articles

MongoDB Python – Insert and Replace Operations

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 20 Jun, 2022

This article focus on how to replace document or entry inside a collection. We can only replace the data already inserted in the database.

Prerequisites : MongoDB Python Basics 

Method used: replace_one() Aim: Replace entire data of old document with a new document

Insertion In MongoDB

We would first insert data in MongoDB. 

Python3




# Python code to illustrate
# Insert in MongoDB
from pymongo import MongoClient
 
try:
    conn = MongoClient()
    print(& quot
           Connected successfully!!!& quot
           )
except:
    print(& quot
           Could not connect to MongoDB & quot
           )
 
# database
db = conn.database
 
# Created or Switched to collection names: my_gfg_collection
collection = db.my_gfg_collection
 
emp_rec1 = {
    & quot
    name & quot: & quot
    Mr.Geek & quot,
    & quot
    eid & quot: 24,
    & quot
    location & quot: & quot
    delhi & quot
}
emp_rec2 = {
    & quot
    name & quot: & quot
    Mr.Shaurya & quot,
    & quot
    eid & quot: 14,
    & quot
    location & quot: & quot
    delhi & quot
}
emp_rec3 = {
    & quot
    name & quot: & quot
    Mr.Coder & quot,
    & quot
    eid & quot: 14,
    & quot
    location & quot: & quot
    gurugram & quot
}
 
# Insert Data
rec_id1 = collection.insert_one(emp_rec1)
rec_id2 = collection.insert_one(emp_rec2)
rec_id3 = collection.insert_one(emp_rec3)
print(& quot
       Data inserted with record ids", rec_id1, & quot
       & quot
       , rec_id2, rec_id3)
 
# Printing the data inserted
cursor = collection.find()
for record in cursor:
    print(record)

Output:

Connected successfully!!!
Data inserted with record ids    
{'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 
'Mr.Geek', 'eid': 24, 'location': 'delhi'}
{'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 
'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}
{'_id': ObjectId('5a02227c37b8552becf5ed2c'), 'name': 
'Mr.Coder', 'eid': 14, 'location': 'gurugram'}

Replace_one()

After inserting the data let’s replace the Data of an employee whose name: Mr.Shaurya 

Matlab




# Python code to illustrate
# Replace_one() in MongoDB
from pymongo import MongoClient
 
try:
    conn = MongoClient()
    print("Connected successfully!!!")
except: 
    print("Could not connect to MongoDB")
 
# database
db = conn.database
 
# Created or Switched to collection names: my_gfg_collection
collection = db.my_gfg_collection
 
# replace one of the employee data whose name is Mr.Shaurya
result = collection.replace_one(
        {"name":"Mr.Shaurya"},
        {
                "name":"Mr.GfG",
                "eid":45,
                "location":"noida"
                 
                }
        )
 
print("Data replaced with id",result)
 
# Print the new record
cursor = collection.find()
for record in cursor:
    print(record)

Output:

Connected successfully!!!
Data replaced with id 
{'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 
'Mr.Geek', 'eid': 24, 'location': 'delhi'}
{'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 
'Mr.GfG', 'eid': 45, 'location': 'noida'}
{'_id': ObjectId('5a02227c37b8552becf5ed2c'), 'name': 
'Mr.Coder', 'eid': 14, 'location': 'gurugram'}

We have successfully replaced the document of employee name:’Mr.Shaurya’ and replaced the entire document with a new one, name:’Mr.GfG’ (present).

To replace multiple documents use update_many() with upsert set to True.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!