0

我在另一个 EmbeddedDocumentList 中有一个带有 EmbeddedDocumentList 的文档,我需要过滤集合,只检索对内部 EmbeddedDocumentList 中的字段具有指定值的文档,我的模型的简化版本是:

class RecipeIngredient(EmbeddedDocument):
    ingredient = ReferenceField(Ingredient, required=True)
    quantity = IntField(required=True)

class RecipeLocalData(EmbeddedDocument):
    price = FloatField(required=True)
    enabled = BooleanField(default=False)
    materialAvailable = BooleanField(default=True)
    ingredients = EmbeddedDocumentListField(RecipeIngredient)

class Recipe(Document):
    localData = EmbeddedDocumentListField(RecipeLocalServerData)
    name = StringField(required=True, unique=True)
    deleted = BooleanField(default=False)

这是存储在集合中的文档样本

 {
 "_id" : ObjectId("55acf8229d5544137f46b2d9"),
"localData" : [
    {
        "price" : NumberLong("14112312313"),
        "enabled" : true,
        "rank" : 12,
        "materialAvailable" : true,
        "ingredients" : [
            {
                "ingredient" : ObjectId("5591948c9d5544c7bd68502e"),
                "quantity" : 1
            }
        ]
    }
],
"name" : "p1",
"deleted" : false,
}

直接在mongodb上我可以通过查询实现这个结果

db.recipe.find({"localData.ingredients.ingredient":ObjectId("5591948c9d5544c7bd68502e")})

使用 mongoengine 我尝试以这种方式进行查询:

Recipe.objects.filter(localData__ingredients__ingredient=ingredient)

Recipe.objects.filter(localData__ingredients__ingredient=ingredient.id)

或使用原始 pymongo 查询

Recipe.objects(__raw__={"localData.ingredients.ingredient":document.id})

但是不工作还有其他选项而不是在应用程序级别进行过滤吗?

4

0 回答 0