3

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.

Jonnyboy
  • 55
  • 1
  • 5

1 Answers1

4

Pass array of sort keys as mentioned in the exception,

for t in collection.find({'raw.lang': 'en'}).sort([( '$natural', 1 )] ):
daemon24
  • 1,388
  • 1
  • 11
  • 23