【问题标题】:Python Mongoengine - get the first document with a non null value in given attributePython Mongoengine - 获取给定属性中具有非空值的第一个文档
【发布时间】:2020-11-19 16:18:13
【问题描述】:

我正在尝试获取给定属性中具有非空值的第一个文档。例如,在 db 中,集合 Fruit 如下所示:

Banana:
  weight: null
  colour: yellow
Apple:
  weight: 100
  colour: green

我希望我的查询返回苹果文档,因为它的权重是一个有效值。我试过了

Fruit.objects.get(weight is not None).first()

但是当我使用邮递员运行我的查询时,我得到了错误:

Not a query object: True. Did you intend to use key=value?

我怎样才能做到这一点?

【问题讨论】:

    标签: python mongoengine


    【解决方案1】:

    您需要使用查询运算符(doc__ne

    Fruit.objects.get(weight__ne=None).first()
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多