关于nexted EmbeddedDocumentLIst字段的mongodb mongoengine过滤器文档

时间:2015-07-20 14:35:29

标签: mongodb pymongo mongoengine embedded-documents

我在其他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)

这是存储在collectiosn

中的文档样本
 {
 "_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})

但不起作用 还有其他选择,而不是在应用程序级别进行过滤?

0 个答案:

没有答案