Using sort with queryable encryption

I have successfully configurated a collection with Queryable Encryption that uses Explicit Encryption. I am able to store new records in the database and I am able to execute find queries.

However, I am not able to do sorting on encrypted fields.

In my example, I have two fields: firstName and lastName, of which lastName is encrypted. The following code returns the expected result, sorted alphabetically.

AggregateIterable<BsonDocument> aggregationResult = collection.aggregate(Arrays.asList(
        Document.parse("{ $sort: { 'firstName': 1 } }")
));

When I try to perform this aggregation on the encrypted field lastName, it does not work. The returned data is not sorted alphabetically. Is there a way to perform sorting on encrypted fields using Explicit Encryption?

I am using the MongoDB Java driver 4.11.1 and MongoDB 7.0.4.

I don’t believe that sort on explicitly encrypted fields is supported. I only see mention in the docs that $sort is supported with automatic encryption with supported drivers… Are you talking about new queryable encryption in 7.x or earlier field level encryption with either explicit or automatic encryption?

Asya

Thank you for the reply. I am indeed talking about the new queryable encryption in 7.x. From the documentation it is not always clear to me what is for automatic encryption and what is for explicit.

My understanding was that encryption and decryption only happen at the client side, while sorting occurs on the server side. Thus my assumption was that there is no difference between explicit or automatic encryption in the aggregation pipeline. But there probably needs to exist an index to allow this sorting?