how to switch collection when query document
regainOWO opened this issue · 1 comments
regainOWO commented
i want to query collection dota_label
and coco_label
which collection field is the same from Labels
classes, only collection name is diffrent, when i use my example is down here, it only get the query result from dota_label collection
from mongoengine import Document, StringField, BinaryField, ListField, FloatField, DictField
class Images(Document):
name = StringField(max_length=256, required=True)
data = BinaryField(required=True)
file_type = StringField(db_field="fileType", default=str())
meta = {
'collection': '_images',
'allow_inheritance': True
}
class Labels(Document):
name = StringField(max_length=256, required=True)
data = ListField(field=DictField(), default=list(dict()))
version = FloatField(default=1.0)
meta = {
'collection': '_labels',
'allow_inheritance': True
}
def main():
# dota_labels collection
Labels._meta.update(collection='dota_labels')
# dota_images collection
Images._meta.update(collection='dota_images')
# query
images_dota = Images.objects()
labels_dota = Labels.objects()
"""change collection to query"""
# coco_labels collection
Labels._meta.update(collection='coco_labels') # no impact
# coco_images collection
Images._meta.update(collection='coco_images') # no impact
# query
images_coco = Images.objects()
labels_coco = Labels.objects()
if __name__ == '__main__':
main()
bagerard commented
There s no built-in support for this and this sounds like a rabbit hole I'd rather not explore.
If you just have a need for a few collections, you can use something like:
class ImageFieldsMixin:
name = StringField(max_length=256, required=True)
class CocoImage(ImageFieldsMixin, Document):
meta = {
'collection': 'coco_images',
}
class DotaImage(ImageFieldsMixin, Document):
meta = {
'collection': 'dota_images',
}