pymongo sort and find_one issue
Question:
I am trying to sort a collection called user_score
using the key position
and get the very first document of the result. In this case the collection user_score
doesn’t exist and I was hoping to get the result as None
, but i was getting a cursor back.
1.
result =
db.user_score.find({'score':'$lt':score}}).sort("position,pymongo.DESCENDING").limit(1)
Now i changed my query like below and did not get anything as expected.
2.
result =
db.user_score.find_one({'score':{'$lt':score}}, sort=[("position", pymongo.DESCENDING)])
What’s the problem with my first query?
Thanks
Answers:
This is the default mongodb behavior on find. Whenever you use find
you get a list of the result (in this case an iterable cursor). Only findOne
– or it’s PyMongo equivalent find_one
will return None
if the query has no matches.
In your first query, in the sort function you’re passing one argument ("position,pymongo.DESCENDING")
, when you should be passing two arguments ("position", pymongo.DESCENDING)
.
Be sure to mind your quotation marks.
Use list to convert the value of the cursor into a dict:
list(db.user_score.find({'score':'$lt':score}}).sort("position",pymongo.DESCENDING).limit(1))[0]
A little late in my response but it appears that the current version of PyMongo does support a sort operation on a find_one call.
From the documentation page here:
All arguments to find() are also valid arguments for find_one(),
although any limit argument will be ignored. Returns a single
document, or None if no matching document is found.
Example usage is as follows:
filterdict = {'email' : '[email protected]'}
collection.find_one(filterdict, sort=[('lastseen', 1)])
Hope this helps more recent searchers!
I am trying to sort a collection called user_score
using the key position
and get the very first document of the result. In this case the collection user_score
doesn’t exist and I was hoping to get the result as None
, but i was getting a cursor back.
1.
result =
db.user_score.find({'score':'$lt':score}}).sort("position,pymongo.DESCENDING").limit(1)
Now i changed my query like below and did not get anything as expected.
2.
result =
db.user_score.find_one({'score':{'$lt':score}}, sort=[("position", pymongo.DESCENDING)])
What’s the problem with my first query?
Thanks
This is the default mongodb behavior on find. Whenever you use find
you get a list of the result (in this case an iterable cursor). Only findOne
– or it’s PyMongo equivalent find_one
will return None
if the query has no matches.
In your first query, in the sort function you’re passing one argument ("position,pymongo.DESCENDING")
, when you should be passing two arguments ("position", pymongo.DESCENDING)
.
Be sure to mind your quotation marks.
Use list to convert the value of the cursor into a dict:
list(db.user_score.find({'score':'$lt':score}}).sort("position",pymongo.DESCENDING).limit(1))[0]
A little late in my response but it appears that the current version of PyMongo does support a sort operation on a find_one call.
From the documentation page here:
All arguments to find() are also valid arguments for find_one(),
although any limit argument will be ignored. Returns a single
document, or None if no matching document is found.
Example usage is as follows:
filterdict = {'email' : '[email protected]'}
collection.find_one(filterdict, sort=[('lastseen', 1)])
Hope this helps more recent searchers!