Reputation: 155
i have a strange problem with my python script. I need to get a totalnumber of database entrys in my MongoDB
>>> a = _chatlog.find()
>>> a.count()
4120
Works just like i planned, yay
Now things get strange:
>>> b = _chatlog.find({ "$query": {}, "$orderby": { "_id" : -1 }})
>>> b.count()
0
I perform this Query because i need all entrys in descending order. First i expected a empty cursor but its not:
for data in b:
c += 1
>>> c
4120
How do i get the count method to work properly when i perform querys?
Upvotes: 1
Views: 432
Reputation: 24007
Count and sort are not compatible. (Nor is it meaningful to combine a sort order with a count.) Remove the $orderby
from your query.
Note that if you use the sort
method instead of including $orderby
, count
will work as expected:
collection.find().sort([('_id', -1)]).count()
This works because count
simply ignores the sort order. I reiterate that it's meaningless to combine sorting and counting.
Upvotes: 2
Reputation: 56
I can't tell you why query modifiers aren't working but they aren't working for me either. Are you using an older version of Mongo?
Is it possible for you to use ".sort()" instead?
From:
b = _chatlog.find({ "$query": {}, "$orderby": { "_id" : -1 }}).count()
To:
b = _chatlog.find({}).sort({_id:-1}).count()
Upvotes: 0