db.test.aggregate({
$match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}}
},
{ $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] }
} });
and avatar returns me null, probably its because its objectId, is it possible in this query to make from this objectId string ?
From MongoDB 4.0 and newer, there is a $toString
operator which 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" }
}
} }
])
This isn't possible yet. WiP issue see: https://jira.mongodb.org/browse/SERVER-29512
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With