I use Djongo package to connect to MongoDB from my Django app. However, there is inconsistency in the query results, even though not expected.
Here is my model:
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
date = models.DateTimeField(db_index=True)
port_inventory_id = models.IntegerField(db_index=True)
This query first filters by port_inventory_id
, then date__range
and returns the correct result
items = Item.objects.filter(port_inventory_id=999999).filter(date__range=(date_from, date_to))
However, this query first filters by date__range
, then port_inventory_id
and returns the same number of items without the filter(port_inventory_id=999999)
items = Item.objects.filter(date__range=(date_from, date_to)).filter(port_inventory_id=999999)
I also tried making one filter call with two filters
items = Item.objects.filter(date__range=(date_from, date_to), port_inventory_id=999999)
And also reverse
items = Item.objects.filter(port_inventory_id=999999, date__range=(date_from, date_to))
The only it works is this:
items = Item.objects.filter(port_inventory_id=999999).filter(date__range=(date_from, date_to))
I did not expect the result to change this way.
Any help is much appeciated…
Faruk Erat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.