MongoEngine / MongoEngine/mongoengine

Why does GenericReferenceField wrap dbref ?

Open
#1,185 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Hi !

In order to migrate some data to GenericReferenceField, I simply used a DBRef as defined in MongoDB documentation: https://docs.mongodb.org/v3.0/reference/database-references/
It took me some time and some code reading to understand that GenericReferenceField wraps DBRef in a {_cls: 'ClassName', _ref: DBRef} object.

So, I'm wondering why is GenericReferenceField using this pattern instead of the standard DBRef. It's harder to query, harder to read and to me the _cls is useless because:

  1. if the referenced document does not allow inheritance, there is only one class in the collection (which dbref provide)
  2. if the referenced document use inheritance, the _cls attribute is stored in the document itself so the collection is sufficient too

At first I was thinking that it permit, in the case of inheritance, to only fetch the required attribute on dereferencing but, it's not allowed anymore to store more field that the model has and this reasoning does not stand for DynamicDocument.

So can someone explain me this choice? Is there some historical reason ? Am I missing something ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with GenericReferenceField's handling of MongoDB DBRef and compare it with the MongoDB database-reference documentation linked in the issue. Determine whether the _cls/_ref wrapper is intentional and document the historical or inheritance-related rationale; the issue is resolved when that rationale is clearly explained.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.