I am trying to add a field in the aggregation pipeline using $addField stage. In the below query after lookup I dont want to unwind the provider_info but want to add a field p_insensitive to be used in sort.
db.getCollection('providers').aggregate([
{"$lookup": {
"localField": "uid",
"from": "users",
"foreignField": "_id",
"as": "provider_info"}},
{"$addFields": {"prov_insensitive": {"$toLower": "$provider_info.full_name"}}},
{"$sort": {"p_insensitive": 1}}
])
It is throwing following error:
can't convert from BSON type array to String
I can not use unwind stage here according to requirement.
Please help me solving this.