i need to search a collection in mongodb in python using below fields targetip=“2.110.0.99”,peerip=“2.210.0.99”,ordertype=“switchover” and state=[“configured”,“configuring”,“verified”].if state is among the 3,we need to raise error.
this is my code for that
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017")
db = client["database_name"]
collection = db["collection_name"]
search_criteria = {
"targetip": "2.110.0.99",
"peerip": "2.210.0.99",
"ordertype": "switchover",
"state": {"$in": ["configured", "configuring", "verified"]}
}
results = collection.find(search_criteria)
for result in results:
if result["state"] in ["configured", "configuring", "verified"]:
raise ValueError("Error: State is among the specified values")
now ,i need to modify the existing query such that if 2 records( one with ordertype=switchover and one with ordertype=switchback) are present with state as verified, error should not come
Error should come only when we are having a record with state verified and ordertype=switchover and we are trying to hit again switchover agian, error should come
or
having a record with state verified and ordertype=switchback and we are trying to hit again switchback agian, error should come
please guide me on how to proceed
thank you