Is there an operator I could use in aggregate function to get a string instead of ObjectId in response?
db.something.aggregate([
{ "$match": { "property": { "$exists": true } } },
{ "$project": { "stringId": "$_id.???" } }
])
Mongodb 4.0 has introduced $toString
aggregation operator. So, Now you can easily convert ObjectId to string
db.collection.aggregate([
{
$project: {
_id: {
$toString: "$_id"
}
}
}
])
OR vice versa using $toObjectId
aggregation
db.collection.aggregate([
{
$project: {
_id: {
$toObjectId: "$_id"
}
}
}
])