In the FIND task, sorting is not performed on fields used as aliases in the project

In the FIND task, sorting is not performed on fields used as aliases in the project.

example)
db..find({}, { field1 : {$size : {“$Arrayfield”}}}).sort(field1:-1)

Field1 is a projection field for counting numbers that does not exist in the document.
Field1 values ​​are output normally, but the final sorting operation is not performed.

I know how to write an aggregate pipeline, but I wonder if I can process it all at once with the Find Operator.

I would like to know information about my problem or technical support.

I do not think you can sort on a computed field with find.

In the find() documentation it is mentioned that

The order of the documents returned in the result set. Fields specified in the sort, must have an index.

But that is for find( query , projection , options ) but since I supected that find( query , projection ).sort() was simply 2 syntactic variations of the same server command. I ran explain() and both sends the same command to the server.

You have no choice but to go with aggregation. There would be not performance gain anyway with find() compare to aggregate().

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.