Using $natural sort in pymongo - MongoDB

Viewed 2480

I understand that using the $natural sort in MongoDB can improve disk throughput as the cursor will read sequential records rather than jumping around.

However when I try to use this SO example or follow the Cursor.sort() Ref Docs I get the following errors:

for t in collection.find({'raw.lang': 'en'}).sort( { $natural: 1 } ):

Unsuprisingly gives a syntax error.

for t in collection.find({'raw.lang': 'en'}).sort( { '$natural': 1 } ):

Exception has occurred: TypeError if no direction is specified, key_or_list must be an instance of list

Any help would be much appreciated. Thanks.

1 Answers

Pass array of sort keys as mentioned in the exception,

for t in collection.find({'raw.lang': 'en'}).sort([( '$natural', 1 )] ):
Related