I have two field name, email in an array format
email:[abc@gmail.com,123@gmail.com]
name:[abc,123]
I want to write it in this format
[abc:abc@gmail.com,123:123@gamil.com]
How can I use aggregation for that ,pls help
I have two field name, email in an array format
email:[abc@gmail.com,123@gmail.com]
name:[abc,123]
I want to write it in this format
[abc:abc@gmail.com,123:123@gamil.com]
How can I use aggregation for that ,pls help
Hello @Abhishek_Upadhyay1, Welcome to the MongoDB community forum,
You can use something like this pipeline,
$map
to iterate loop of email
$indexOfArray
to get the array index of the email
$arrayElemAt
to get a specific element by using its array index from name
$arrayToObject
converts the above array of objects into an object$replceRoot
to replace the above object in the rootdb.collection.aggregate([
{
$project: {
items: {
$map: {
input: "$email",
in: {
k: {
$arrayElemAt: [
"$name",
{ $indexOfArray: ["$email", "$$this"] }
]
},
v: "$$this"
}
}
}
}
},
{
$replaceRoot: {
newRoot: { $arrayToObject: "$items" }
}
}
])