Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask mongongine not to use _cls in queries #2749

Open
kietheros opened this issue Apr 6, 2023 · 2 comments
Open

Ask mongongine not to use _cls in queries #2749

kietheros opened this issue Apr 6, 2023 · 2 comments

Comments

@kietheros
Copy link

class BaseEvent(Document):
	type = IntField(required=True)
	source_id = IntField(required=True)

	meta = {
        'allow_inheritance': True,
        'collection': 'event',
    }


class SimpleEvent(BaseEvent):
    start_time = DateTimeField(required=True)
    end_time = DateTimeField(required=True)
	
	meta = {
        'allow_inheritance': True,
        'collection': 'event',
    }

# event type = 1
class EventA(BaseEvent):
 	pass


# event type = 2
class EventB(SimpleEvent):
 	x = IntField(required=True)
 	# ........................

I have many types of events, all events are saved in collection 'event'.
I want to define documents:

  • Use inheritance for define event class (for control fields which each event has)
  • Use field type for querying event of a type
  • Ask mongongine not to use _cls in all queries ?
@bagerard
Copy link
Collaborator

bagerard commented Apr 6, 2023

what's your actual problem with Mongoengine using _cls in all queries? When querying from a subclass, e.g EventB.objects.first(), _cls is used to filter on that specific "EventB" class so removing it wouldn't make much sense as it would make it return another type of object... You can query the parent class if you don't want _cls

@kietheros
Copy link
Author

@bagerard
Query: List all events in a source_ids
BaseEvent.objects.filter(source_id__in=[1, 2 , 3], ...)

Generate query:
('filter', {'source_id': {'$in': [1,2,3]}, '_cls': {'$in': ('BaseEvent', 'BaseEvent.SimpleEvent', 'BaseEvent.EventA', 'BaseEvent.SimpleEvent.EventB')}})
Runtime: 0.9s

While I only need to filter by source_id
('filter', {'source_id': {'$in': [1,2,3]}}})
Run time: 0.5s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants