Skip to content

Ask mongongine not to use _cls in queries  #2749

Open

Description

@kietheros
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 ?

Activity

  1. bagerard commented on Apr 6, 2023

    @bagerard
    Collaborator

    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

  2. kietheros commented on Apr 6, 2023

    @kietheros
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions