Insert without having duplicate documents

Hello, im making a python API that communicates via http with others APIs, which sends/recieves several messagens that are serialized/deserialized to several objects. Those objects were auto generated by openapi codegen and dont have ID fields. so when im trying to save the objects to mongo the ids are auto generated. Now my database is persistence so every time i restart my API it recieves messages and needs to insert or update only the data that is new or was altered. The problem is that it duplicates the documents. How can i avoid this issue. (im using pymongo and motor asyncio clients)

the nfInstanceId field in the documents can be used as primary key