MongoDB Python–插入和替换操作

先决条件: MongoDB Python基础知识 本文主要讨论如何替换集合中的文档或条目。我们只能替换数据库中已经插入的数据。 使用的方法: 替换一个()和替换多个() 目标:用新文档替换旧文档的全部数据

null

MongoDB中的插入

我们首先在MongoDB中插入数据。

# Python code to illustrate
# Insert 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
emp_rec1 = {
"name" : "Mr.Geek" ,
"eid" : 24 ,
"location" : "delhi"
}
emp_rec2 = {
"name" : "Mr.Shaurya" ,
"eid" : 14 ,
"location" : "delhi"
}
emp_rec3 = {
"name" : "Mr.Coder" ,
"eid" : 14 ,
"location" : "gurugram"
}
# Insert Data
rec_id1 = collection.insert_one(emp_rec1)
rec_id2 = collection.insert_one(emp_rec2)
rec_id3 = collection.insert_one(emp_rec3)
print ( "Data inserted with record ids" ,rec_id1, " " ,rec_id2,rec_id3)
# Printing the data inserted
cursor = collection.find()
for record in cursor:
print (record)


输出:

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'}

替换_one()

插入数据后,让我们替换名为Shaurya先生的员工的数据

# 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)


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'}

我们已成功地替换了员工姓名为“Mr.Shaurya”的文件,并将整个文件替换为新文件,名称为“Mr.GfG”(目前)。

替换许多

考虑到数据与插入的数据相同。 将所有数据项替换为eid:14。

# Python code to illustrate
# Replace_many() 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_many(
{ "eid" : 14 },
{
"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)


产出应该是:

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.GfG', 'eid': 45, 'location': 'noida'}

在这里,我们可以看到eid:14的两个条目被新数据替换。(即使数据相同,ObjectId也会不同)。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享