Mongoengine: Renaming of "_cls" key

Created on 16 Mar 2020  路  5Comments  路  Source: MongoEngine/mongoengine

Hi all,

for schema inheritance with polymorphic models (allow_inheritance=True) the field "_cls" will be added to the saved document. This field contains by default the class name, but can be overridden by:

class MyDocument(mongoengine.Document):
def __init__(self, kwargs):
kwargs['_cls'] = 'MyOtherDocument'
super().__init__(
kwargs)

So far so good.

In the mongoose ODM for Javascript this is called a discriminator, by default it's "__t" (instead of "_cls" in mongoengine). But mongoose offers the possibility to rename this key "__t" by setting a "descriminatorKey" property.

1) Does mongoengine offers this possibility of renaming the discriminator key too?

2) When setting the value for "_cls" to something different than the class name (as mentioned above), querying the collection fails, because "_cls" will be added to the query using the class name, no chance to set it differently. Any hint for that?

Best regards.

Most helpful comment

Yet another request for being able to configure the discriminator: We are using the schema versioning pattern and have different data classes per type and version. We'd like to be able to dispatch to the concrete version subtype based on a field called schemaVersion, which isn't possible right now. Or do you propose any other pattern to handle schema versioning in mongoengine?

All 5 comments

Hi @msanders70, MongoEngine offers no support for:
A) customizing the discriminator key (e.g: rename "_cls" key to "_my_funky_key")
B) customizing the discriminator value (e.g renaming the class name as you do in your example)

And I wouldn't advise to try to workaround that... _cls gets used behind the scene at least for the indexes, and the queries

From what I understand you want to achieve both, right?
I'm curious but could you clarify your use case? Perhaps you have a database that was initially created with mongoose and you know need to read it from python?

Hi @bagerard,

thanks for your reply!

Indeed, there is one DB used from two sides (mongoose and mongoengine). The mongoose side uses another discriminator key than the mongoengine side.
My current workaround is to NOT modify the "_cls" property value (good hint) and to add a new field with a key called like the mongoose side's discriminator key. I'm not sure if this works, but I'll give it a try.

Or do you have another suggestion?

That sounds like a possible workaround.
I don't know about your application's complexity or size but targeting mongoDB from 2 ORM's can be challenging to maintain on the long run.

Thank you very much!

Yet another request for being able to configure the discriminator: We are using the schema versioning pattern and have different data classes per type and version. We'd like to be able to dispatch to the concrete version subtype based on a field called schemaVersion, which isn't possible right now. Or do you propose any other pattern to handle schema versioning in mongoengine?

Was this page helpful?
0 / 5 - 0 ratings