1

最近、おそらくソフトウェアのアップグレード後に、問題に遭遇しました。distinct() フィルタリングが、EmbeddedDocument コレクションではなく辞書コレクションを返すようになりました。オブジェクトの直接トラバースは期待どおりに機能しますが。

これがコードの一部です。おそらく削除されたものではなく、むしろ単純です。これは、機能によるフィルタリングなどの機能を備えた単純な製品カタログです。

from mongoengine import *


class FeatureInfo(Document):
    title = StringField()


class ProductFeature(EmbeddedDocument):
    featureInfo = ReferenceField(FeatureInfo)
    value = StringField()

    def eq(a, b):
        return a.featureInfo.title == b.featureInfo.title and a.value == b.value


class Product(Document):
    title = StringField()
    features = ListField(EmbeddedDocumentField(ProductFeature))


def populate():
    producer_feature_info = FeatureInfo("Producer")
    producer_feature_info.save()

    screen_size_feature_info = FeatureInfo("Screen size")
    screen_size_feature_info.save()

    iphone_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Apple")
    iphone_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="4 inch")

    iphone = Product(title="iPhone 5S")
    iphone.features.append(iphone_producer_feature)
    iphone.features.append(iphone_screen_size_feature)
    iphone.save()

    galaxy_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Samsung")
    galaxy_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="5 inch")

    galaxy = Product(title="Galaxy S4")
    galaxy.features.append(galaxy_producer_feature)
    galaxy.features.append(galaxy_screen_size_feature)
    galaxy.save()

    galaxy_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Samsung")
    galaxy_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="4 inch")

    galaxy = Product(title="Galaxy S2")
    galaxy.features.append(galaxy_producer_feature)
    galaxy.features.append(galaxy_screen_size_feature)
    galaxy.save()


if __name__ == "__main__":
    connect('test')
    populate()

    products = Product.objects()

    print "\n".join(map(lambda p: p.title + " [" + ", ".join(map(lambda f: f.featureInfo.title + " = " + f.value, p.features)) + "]", products))

    print "\nProduct features"

    features = []

    for product in products:
        print product.title
        for feature in product.features:
            if feature not in features:
                features.append(feature)
            print feature.__class__.__name__

    print "\nUnique features"

    for feature in features:
        print feature.featureInfo.title + " = " + feature.value + " (" + feature.__class__.__name__ + ")"

    print "\nDistinct features"

    features = products.distinct("features")

    for feature in features:
        print feature.__class__.__name__

出力は次のとおりです。

python test.py 
iPhone 5S [Producer = Apple, Screen size = 4 inch]
Galaxy S4 [Producer = Samsung, Screen size = 5 inch]
Galaxy S2 [Producer = Samsung, Screen size = 4 inch]

Product features
iPhone 5S
ProductFeature
ProductFeature
Galaxy S4
ProductFeature
ProductFeature
Galaxy S2
ProductFeature
ProductFeature

Unique features
Producer = Apple (ProductFeature)
Screen size = 4 inch (ProductFeature)
Producer = Samsung (ProductFeature)
Screen size = 5 inch (ProductFeature)

Distinct features
BaseDict
BaseDict
BaseDict
BaseDict

StackOverflow でそのような質問を見つけましたが、MongoEngine 0.8.3 に問題があり、0.8.4 では問題ありませんでした。私はそれに従ってアップグレードしましたが、効果はありません。

4

0 に答える 0