Welcome to the MongoDB Community ![]()
Could you please double-check that you are running this query on the encrypted collection? Because looking at the error it seems you are running on the encrypted field.
While executing the above aggregation query {'$group': { _id: '$deviceCode', latestDocument: { '$first': '$$ROOT' } } }, you are receiving the error because the $$ROOT and $$CURENT are both currently unavailable when using field-level encryption. This is because the encrypted field mappings would have the effect of projecting encrypted values into new field paths that are inconsistent with the existing key mappings, due to which you will not be able to decrypt the field in the new document shape.
I hope it helps. Please let us know if you have any additional questions or concerns regarding the above details.
Best regards,
Kushagra