I am trying to find a document in a MongoDB instance with the largest value of a particular field.
Here is the current code I have to do that:
document = collection.find().sort('experiment_id', -1).limit(1)
if document is None:
# ...
else:
return document['experiment_id']
I am using this query format (find()
followed by sort()
combined with limit(1)
because this is supposed to be interpreted by Mongo in a highly optimized way.
However, this does not work because document
is a cursor type, therefore one cannot call ['experiment_id']
on it.
What I find slightly suprising is that limit(1)
returns a cursor object and not a single value or None
type.
How should I write a pymongo query which finds the largest value of the field 'experiment_id'
from a collection of documents?
It seems obvious that the following will work but it doesn’t seem like a particularly good solution.
for document in documents:
return document['experiment_id']
2
Answers
Since you want only one result – as a document, rather than a cursor, you could use
find_one()
with asort
criteria which behaves like it does as a param tofind()
, and drop thelimit
clause, since it will be ignored anyway.Additionally, if you want just the ‘experiment_id’ and none of the rest, you can use a
projection
which would return a document with only the specified fields.Your code
returns a cursor.
collection
‘sfind
always returns acursor
(irrespective of the number of documents thefind
returns). In case there is no result document the cursor will be empty with zero documents.You can perform many operations on a cursor, and one of them is to iterate the cursor. Using this,
References: