Query regarding finding specific records in db

i need to search a collection in mongodb in python using below fields targetip=“”,peerip=“”,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": "",
    "peerip": "",
    "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
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

Why not group by the non-state criteria and then you can just check if you have switchover and switchback in the output?

Touch brief, so:

    "targetip": "",
    "peerip": "",
    "ordertype": "switchover",