MongoEngine/mongoengine

Ask mongongine not to use _cls in queries

kietheros opened this issue · 2 comments

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 ?

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

@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