MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.26k stars 1.23k forks source link

`id` as a unique field alongside `_id` #2819

Closed iebb closed 3 months ago

iebb commented 5 months ago

Migrating from Djongo here, and my current data involves - _id: the automatic ObjectId field id: Django-style integer field with a unique index.

As I want to maintain compatibility between Djongo model and MongoEngine, and since MongoEngine relies heavily on the 'id' field, what's the current best practice for MongoEngine to maintain both?

bagerard commented 3 months ago

As you pointed out, id is kind of an alias for _id in MongoEngine and I don't have a workaround (and don't think it's such a common scenario to develop one)

One thing you can consider is to use your django style integer as id

class YourDoc(Document):
    id = IntegerField(primary_key=True)
    ...