Starting with MongoDB 4.0 and later, there is a $toString statement that returns the ObjectId value as a hexadecimal string:
db.test.aggregate([ { "$match": { "themType": "SuperTest", "mType": { "$in" : [1 , 2] } } }, { "$addFields": { "avatar": { "$toString": "$refTestId" } } } ])
or using $convert
db.test.aggregate([ { "$match": { "themType": "SuperTest", "mType": { "$in" : [1 , 2] } } }, { "$addFields": { "avatar": { "$convert": { "input": "$refTestId", "to": "string" } } } } ])
source share