Mongodb aggregation project object Excluded with concat

db.test.aggregate({ $match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}} }, { $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] } } }); 

and the avatar returns null to me, perhaps because its objectId, is it possible to make objectId from this string in this request?

+8
source share
2 answers

It is not possible yet. WiP issue: https://jira.mongodb.org/browse/SERVER-29512

+1
source

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" } } } } ]) 
+1
source

Source: https://habr.com/ru/post/976025/


All Articles