Search code examples
documentmongoengineembedded-documents

MongoEngine: Getting parent document from embedded document


I am currently trying to write a generic deletion method for embedded documents in MongoEngine. I thought it would be possible to access the document object that contains an embedded document and define the delete function based on the parent document.

The delete function could then possibly look like this: def delete(): {parent_document}.update(pull__myField={self})

I'm not quite sure yet if this will really work the way I imagine it will but I think it's worth a try. The only thing I don't know yet is how to access the document in which the embedded document exists. So basically I have to somehow access the document referenced as parent_document in the code above from the embedded document.

Does anyone have any idea how to do this? Maybe it's a bad idea in general if someone thinks so, just let me know.


Solution

  • If you want to access to parent document, you can use self._instance in embedded document.

    https://github.com/MongoEngine/mongoengine/issues/63