F
F
froosty2016-02-18 12:26:11
Python
froosty, 2016-02-18 12:26:11

How to make a request in mongoalchemy, with the condition of equality of 2 fields of the document?

Good day. I have a model for mongoalchemy:

class ImportProductReport(mongo.Document):
    target = mongo.IntField(default=0)
    processed = mongo.IntField(default=0)
    is_in_process_remove_offers = mongo.BoolField(default=False)
    is_old_offers_removed = mongo.BoolField(default=False)
    ...

I need to pull records for which:
- ImportProductReport.is_in_process_remove_offers == False,
- ImportProductReport.is_old_offers_removed == False,
- ImportProductReport.target == ImportProductReport.processed.
The query without the last condition works well:
res = ImportProductReport.query\
    .filter(
        ImportProductReport.is_old_offers_removed == False,
        ImportProductReport.is_in_process_remove_offers == False
    )\
    .all()

But if you write something like this:
res = ImportProductReport.query\
    .filter(
        ImportProductReport.is_old_offers_removed == False,
        ImportProductReport.is_in_process_remove_offers == False,
        ImportProductReport.target == ImportProductReport.processed
    )\
    .all()

Then we get an error:
AttributeError: 'bool' object has no attribute 'obj'

Tell me how to add the condition "one field of the document is equal to the second field of the document." Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
froosty, 2016-02-18
@froosty

If suddenly someone will help, then I found the answer to my question.
Mongoalchemy allows queries to be made with a syntax similar to the original MongoDB syntax.
Therefore, the query I need can be written as follows:

res = ImportProductReport.query\
    .filter({
        'is_old_offers_removed': False,
        'is_in_process_remove_offers': False,
        '$where': 'this.target == this.processed'
    })\
    .all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question