have a collection with data
date_new:Array
Using mongodb aggregation how to convert into string like this, if there is a empty string convert it into " "
date_new:Array
0: " "
Please try this :
db.yourCollectionName.aggregate([{
$addFields:
{
date_new: {
$arrayToObject:
{
$map:
{
input: "$date_new",
as: "each",
in: [{ $toString: { $indexOfArray: ["$date_new", '$$each'] } }, '$$each']
}
}
}
}
}])
Collection Data :
/* 1 */
{
"_id" : ObjectId("5e1e2a74d3c98f2a7100fd44"),
"date_new" : [
"abc",
"def",
""
]
}
/* 2 */
{
"_id" : ObjectId("5e1e2f25d3c98f2a7100fd45"),
"date_new" : []
}
Result :
/* 1 */
{
"_id" : ObjectId("5e1e2a74d3c98f2a7100fd44"),
"date_new" : {
"0" : "abc",
"1" : "def",
"2" : ""
}
}
/* 2 */
{
"_id" : ObjectId("5e1e2f25d3c98f2a7100fd45"),
"date_new" : {}
}
date_new : [0:'', 1:''], So it has to be an array of objectsdate_new : [ {0:' '},{ 1:' '} ]or with object & it's elements like below result. Also what do you mean byif there is a empty string convert it into " "?? We're converting array how come there will be empty string ? Do you mean an element indate_newas empty string ?