Biu
Biu

Reputation: 67

Python Mongoengine - get the first document with a non null value in given attribute

I'm trying to get the first document with a non null value in given attribute. For example, in the db the collection Fruit looks like this:

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

I want my query to return the apple document because its weight a valid value. I tried

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

but when running my query with postman I get the error:

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

How can I achieve this ?

Upvotes: 0

Views: 187

Answers (1)

bagerard
bagerard

Reputation: 6364

You need to use the query operator (doc) __ne :

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

Upvotes: 1

Related Questions