匹配 EmbeddedDocumentList 中的 EmbeddedDocument
Matching EmbeddedDocument in EmbeddedDocumentList
所以我在 mongo 中有这个测试数据,购物车型号:
{
"_id" : ObjectId("55eb513c516ddc8fa6e68886"),
"user" : ObjectId("55e3f236516ddc78296968be"),
"items" : [
{
"item" : ObjectId("55eb10b8516ddc7508dba7c2"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25"),
"order_type" : [
"in_store",
"curbside"
]
},
{
"item" : ObjectId("55eb10b8516ddc7508dba7cc"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25")
},
{
"item" : ObjectId("55eb10b8516ddc7508dba7c8"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25")
}
]
}
这是我的模型:
class CartItem(mongoengine.EmbeddedDocument):
item = mongoengine.ReferenceField('Item')
quantity = mongoengine.IntField()
added_date = mongoengine.DateTimeField( default=timezone.now() )
coupons = mongoengine.ReferenceField('Coupon')
order_type = mongoengine.ListField(mongoengine.StringField(choices=ORDER_TYPE_CHOICES))
class Cart(mongoengine.Document):
user = mongoengine.ReferenceField('User')
items = mongoengine.EmbeddedDocumentListField(CartItem)
savings = mongoengine.DecimalField(precision=2, default=0)
现在我希望能够获得 CartItems,例如只有 order_type of "in_store" .所以我是这样的:
cart = Cart.objects.filter(id="id")
items = cart.items
curbside_items = items.filter(order_type="curbside")
这没有 return 任何东西。所以我试试这个:
curbside_items = items.filter(order_type__math="curbside")
但这对 return 我没有任何意义,因为 well.So 我的问题是我怎样才能在 mongoengine.Thank 你身上做到这一点!
MongoEngine 使用 "double underscore" for properties in translation to the "dot notation" 形式的 MongoDB 查询。所以你要找的 属性 的 "parent" 是 "items" ,这意味着你用完整的路径引用如下:
Cart.objects.filter(items__order_type="in_store")
其中文档包含 "in_store" 作为 items
数组中的 order_type
条目。请注意,此 returns "whole documents" 而不仅仅是匹配条目。还有其他方法可以在响应中处理该问题。
一种方法是使用底层 pymongo 驱动程序得到 "raw" 结果:
Cart._get_collection().find(
{ "items.order_type": "in_store" },
{ "items.$": 1 }
)
它利用 positional $
运算符仅 returned 匹配的数组元素。
或者 "mutiple" 匹配然后你需要 .aggregate()
:
Cart._get_collection().aggregate([
{ "$match": {
"items.order_type": "in_store"
}},
{ "$redact": {
"$cond": {
"if": { "$eq": [ { "$ifNull": [ "$order_type", "in_store" ] }, "in_store" ] },
"then": "$$DESCEND",
"else": "$$PRUNE"
}
}}
])
或者通过 $filter
:
在未来的版本中更有意义
Cart._get_collection().aggregate([
{ "$match": {
"items.order_type": "in_store"
}},
{ "$project": {
"items": {
"$filter": {
"input": "$items",
"as": "el",
"cond": { "$eq": [ "$$el.order_type", "in_store" ] }
}
}
}}
])
"djangoesque" 行中的标准 mongoengine 查询不支持此类操作,只有 return 整个文档未修改。 $slice
例外,其中有一个选项可以执行其本机 MongoDB 对应项所做的事情。
所以我在 mongo 中有这个测试数据,购物车型号:
{
"_id" : ObjectId("55eb513c516ddc8fa6e68886"),
"user" : ObjectId("55e3f236516ddc78296968be"),
"items" : [
{
"item" : ObjectId("55eb10b8516ddc7508dba7c2"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25"),
"order_type" : [
"in_store",
"curbside"
]
},
{
"item" : ObjectId("55eb10b8516ddc7508dba7cc"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25")
},
{
"item" : ObjectId("55eb10b8516ddc7508dba7c8"),
"quantity" : 1,
"added_date" : ISODate("2015-09-05T20:32:16.527Z"),
"coupons" : ObjectId("55eb10cd516ddc751d3d5e25")
}
]
}
这是我的模型:
class CartItem(mongoengine.EmbeddedDocument):
item = mongoengine.ReferenceField('Item')
quantity = mongoengine.IntField()
added_date = mongoengine.DateTimeField( default=timezone.now() )
coupons = mongoengine.ReferenceField('Coupon')
order_type = mongoengine.ListField(mongoengine.StringField(choices=ORDER_TYPE_CHOICES))
class Cart(mongoengine.Document):
user = mongoengine.ReferenceField('User')
items = mongoengine.EmbeddedDocumentListField(CartItem)
savings = mongoengine.DecimalField(precision=2, default=0)
现在我希望能够获得 CartItems,例如只有 order_type of "in_store" .所以我是这样的:
cart = Cart.objects.filter(id="id")
items = cart.items
curbside_items = items.filter(order_type="curbside")
这没有 return 任何东西。所以我试试这个:
curbside_items = items.filter(order_type__math="curbside")
但这对 return 我没有任何意义,因为 well.So 我的问题是我怎样才能在 mongoengine.Thank 你身上做到这一点!
MongoEngine 使用 "double underscore" for properties in translation to the "dot notation" 形式的 MongoDB 查询。所以你要找的 属性 的 "parent" 是 "items" ,这意味着你用完整的路径引用如下:
Cart.objects.filter(items__order_type="in_store")
其中文档包含 "in_store" 作为 items
数组中的 order_type
条目。请注意,此 returns "whole documents" 而不仅仅是匹配条目。还有其他方法可以在响应中处理该问题。
一种方法是使用底层 pymongo 驱动程序得到 "raw" 结果:
Cart._get_collection().find(
{ "items.order_type": "in_store" },
{ "items.$": 1 }
)
它利用 positional $
运算符仅 returned 匹配的数组元素。
或者 "mutiple" 匹配然后你需要 .aggregate()
:
Cart._get_collection().aggregate([
{ "$match": {
"items.order_type": "in_store"
}},
{ "$redact": {
"$cond": {
"if": { "$eq": [ { "$ifNull": [ "$order_type", "in_store" ] }, "in_store" ] },
"then": "$$DESCEND",
"else": "$$PRUNE"
}
}}
])
或者通过 $filter
:
Cart._get_collection().aggregate([
{ "$match": {
"items.order_type": "in_store"
}},
{ "$project": {
"items": {
"$filter": {
"input": "$items",
"as": "el",
"cond": { "$eq": [ "$$el.order_type", "in_store" ] }
}
}
}}
])
"djangoesque" 行中的标准 mongoengine 查询不支持此类操作,只有 return 整个文档未修改。 $slice
例外,其中有一个选项可以执行其本机 MongoDB 对应项所做的事情。