Esercizi Eliminazione MongoDB Python
Codegrind Team•Jul 10 2024
Esercizi sull’eliminazione di dati in un database MongoDB utilizzando le librerie Python pymongo
e motor
. Gli esercizi includono approcci sequenziali e orientati agli oggetti (OOP).
Esercizio 1: Eliminazione Singola con pymongo (Sequenziale)
Eliminare un singolo documento da una collezione utilizzando pymongo.
from pymongo import MongoClient
def delete_single_document():
client = MongoClient('mongodb://localhost:27017/')
db = client['testdb']
collection = db['employees']
result = collection.delete_one({"name": "John Doe"})
print("Documenti eliminati:", result.deleted_count)
client.close()
delete_single_document()
Esercizio 2: Eliminazione Multipla con pymongo (OOP)
Eliminare più documenti da una collezione utilizzando pymongo e OOP.
from pymongo import MongoClient
class MongoDBConnection:
def __init__(self, uri, db_name, collection_name):
self.client = MongoClient(uri)
self.db = self.client[db_name]
self.collection = self.db[collection_name]
def delete_multiple_documents(self, query):
result = self.collection.delete_many(query)
print("Documenti eliminati:", result.deleted_count)
def close(self):
self.client.close()
# Uso
db_connection = MongoDBConnection('mongodb://localhost:27017/', 'testdb', 'employees')
db_connection.delete_multiple_documents({"position": "Intern"})
db_connection.close()
Esercizio 3: Eliminazione Singola con motor (Sequenziale)
Eliminare un singolo documento da una collezione utilizzando motor.
import motor.motor_asyncio
import asyncio
async def delete_single_document():
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost:27017/')
db = client['testdb']
collection = db['employees']
result = await collection.delete_one({"name": "Jane Doe"})
print("Documenti eliminati:", result.deleted_count)
client.close()
# Uso
asyncio.run(delete_single_document())
Esercizio 4: Eliminazione Multipla con motor (OOP)
Eliminare più documenti da una collezione utilizzando motor e OOP.
import motor.motor_asyncio
import asyncio
class MongoDBConnection:
def __init__(self, uri, db_name, collection_name):
self.client = motor.motor_asyncio.AsyncIOMotorClient(uri)
self.db = self.client[db_name]
self.collection = self.db[collection_name]
async def delete_multiple_documents(self, query):
result = await self.collection.delete_many(query)
print("Documenti eliminati:", result.deleted_count)
def close(self):
self.client.close()
# Uso
async def main():
db_connection = MongoDBConnection('mongodb://localhost:27017/', 'testdb', 'employees')
await db_connection.delete_multiple_documents({"position": "Contractor"})
db_connection.close()
asyncio.run(main())
Esercizio 5: Eliminazione Condizionale con pymongo (Sequenziale)
Eliminare documenti con una condizione specifica utilizzando pymongo.
from pymongo import MongoClient
def delete_conditional_documents():
client = MongoClient('mongodb://localhost:27017/')
db = client['testdb']
collection = db['employees']
result = collection.delete_many({"years_of_experience": {"$lt": 1}})
print("Documenti eliminati:", result.deleted_count)
client.close()
delete_conditional_documents()
Esercizio 6: Eliminazione con Indice con pymongo (OOP)
Eliminare un documento utilizzando un indice specifico con pymongo e OOP.
from pymongo import MongoClient
class MongoDBConnection:
def __init__(self, uri, db_name, collection_name):
self.client = MongoClient(uri)
self.db = self.client[db_name]
self.collection = self.db[collection_name]
def delete_document_by_id(self, document_id):
result = self.collection.delete_one({"_id": document_id})
print("Documenti eliminati:", result.deleted_count)
def close(self):
self.client.close()
# Uso
from bson.objectid import ObjectId
db_connection = MongoDBConnection('mongodb://localhost:27017/', 'testdb', 'employees')
db_connection.delete_document_by_id(ObjectId("64b61ec69c77c5c3f1a267c9"))
db_connection.close()
Esercizio 7: Eliminazione Condizionale con motor (Sequenziale)
Eliminare documenti con una condizione specifica utilizzando motor.
import motor.motor_asyncio
import asyncio
async def delete_conditional_documents():
client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost:27017/')
db = client['testdb']
collection = db['employees']
result = await collection.delete_many({"age": {"$gte": 60}})
print("Documenti eliminati:", result.deleted_count)
client.close()
# Uso
asyncio.run(delete_conditional_documents())
Esercizio 8: Eliminazione con Indice con motor (OOP)
Eliminare un documento utilizzando un indice specifico con motor e OOP.
import motor.motor_asyncio
import asyncio
from bson.objectid import ObjectId
class MongoDBConnection:
def __init__(self, uri, db_name, collection_name):
self.client = motor.motor_asyncio.AsyncIOMotorClient(uri)
self.db = self.client[db_name]
self.collection = self.db[collection_name]
async def delete_document_by_id(self, document_id):
result = await self.collection.delete_one({"_id": document_id})
print("Documenti eliminati:", result.deleted_count)
def close(self):
self.client.close()
# Uso
async def main():
db_connection = MongoDBConnection('mongodb://localhost:27017/', 'testdb', 'employees')
await db_connection.delete_document_by_id(ObjectId("64b61ec69c77c5c3f1a267c9"))
db_connection.close()
asyncio.run(main())