Sadderdaze
Sadderdaze

Reputation: 314

Mongoengine find document without a field

I need to find a document in the collection of Chat that contains no deal field. According to the documentation exists checks if the field exist, and first, is used to retrieve the first result, but the next line

Chat.objects.first(deal__exists=False)

raises TypeError: first() got an unexpected keyword argument 'deal__exists'

That's the Chat scheme

class Chat(Document):
    id = IntField(primary_key=True)
    name = StringField(default=CHANNEL_NAME)
    invite_link = StringField(default=None)
    deal = ReferenceField("Deal", default=None)

and current state of the only document in the collection:

{
    "_id" : XXXXXXXXX,
    "name" : "XXXXX",
    "invite_link" : "https://t.me/joinchat/XXXXXXXXXXXXXXXXXXXXXX"
}

Value none for the deal gives the same result

Upvotes: 0

Views: 2052

Answers (2)

Sadderdaze
Sadderdaze

Reputation: 314

Took me a while.

First of all, method first should be applied to the result of Chat.objects(deal=None). Just like Chat.objects(deal=None).first().

And it should not be used in with statement as it raises AttributeError: __enter__. Here are more details on the error

Upvotes: 0

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

You can try :

Chat.objects.get(deal__exists=False)

or

Chat.objects(deal__exists=False)[0]

to find the first result.

Upvotes: 2

Related Questions