com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'must have $meta projection for all $meta sort keys' on server cluster0-shard-00-02-1puv5.mongodb.net:27017

When i m creating a sort for search by cast, i m getting this issue. I dont understand what this actually means!

Never seen that but the message is pretty clear, eXspecially together what is written in the documentation . you need to explicitly mention $meta in the projection document - the one where you define which fields should be projected/returned before you can sort on it

From documentation:
o include a $meta expression in a sort() expression, the same $meta expression, including the <projectedFieldName> , must appear in the projection document.

i m using one of the Mongo’s custom class:
String sortKey- user Input
Bson sort = Sorts.metaTextScore(sortKey);
This is my statement.
Where “metaTextScore” is a method from Momgo’s Sort Class:

public static Bson metaTextScore(String fieldName) {
return new BsonDocument(fieldName, new BsonDocument("$meta", new BsonString(“textScore”)));
}.

Hi @Nithyabharathy,

Can you share the code for the whole function?

Kanika