mongoengine - 查询 EmbeddedDocumentField 的 ListField [英] mongoengine - Query on ListField of EmbeddedDocumentField

查看:33
本文介绍了mongoengine - 查询 EmbeddedDocumentField 的 ListField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Django 和 python 中使用 mongoengine.

I use mongoengine with Django and python.

这是我的代码:

class Chambre(EmbeddedDocument):
    max_personne = IntField(default=0)
    prix = IntField(default=0)

class Hotel(Document):
    code = IntField(default=0)
    nom = StringField(max_length=200)
    chambre = ListField(EmbeddedDocumentField(Chambre))
    resume = StringField(max_length=200)

1 - 我想要一个查询来过滤所有 Hotel 至少有一个 Chambre 与 prix >= a(浮动数字)

1 - I want a query to filter all Hotel that have at least a Chambre with prix >= a (a floeat number)

2 - 还有那个 Chambre

有什么想法吗?

推荐答案

您可以使用 嵌入式符号n 以及查询运算符 表示大于或等于"

You can use the embedded notation as well as the Query Operator for "greater than or equal "

Hotel.objects(chambre__prix__gte=a)

或者如果您需要转换为整数:

Or if you need to cast as an integer:

Hotel.objects(chambre__prix__gte=int(math.floor(a)))

如果您只想投影匹配"元素,请直接在驱动程序上使用原始查询:

If you want to only project the "matched" element, use a raw query directly on the driver instead:

Hotel._get_collection().find(
  { 'chambre.prix': { '$gte': int(math.floor(a)) } },
  { 'chambre.$': 1 }
)

这篇关于mongoengine - 查询 EmbeddedDocumentField 的 ListField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆