Python Mongoengine - 获取给定属性中具有非空值的第一个文档
Python Mongoengine - get the first document with a non null value in given attribute
我正在尝试获取给定属性中具有非空值的第一个文档。例如,在数据库中,Fruit 集合如下所示:
Banana:
weight: null
colour: yellow
Apple:
weight: 100
colour: green
我想查询 return 苹果文档,因为它的权重是一个有效值。我试过了
Fruit.objects.get(weight is not None).first()
但是当 运行 我向邮递员查询时,我收到错误消息:
Not a query object: True. Did you intend to use key=value?
我怎样才能做到这一点?
您需要使用查询运算符 (doc) __ne
:
Fruit.objects.get(weight__ne=None).first()
我正在尝试获取给定属性中具有非空值的第一个文档。例如,在数据库中,Fruit 集合如下所示:
Banana:
weight: null
colour: yellow
Apple:
weight: 100
colour: green
我想查询 return 苹果文档,因为它的权重是一个有效值。我试过了
Fruit.objects.get(weight is not None).first()
但是当 运行 我向邮递员查询时,我收到错误消息:
Not a query object: True. Did you intend to use key=value?
我怎样才能做到这一点?
您需要使用查询运算符 (doc) __ne
:
Fruit.objects.get(weight__ne=None).first()