I am trying to merge two fields here is my collection
db.Acc_Details.findOne()
{
"_id": ObjectId("577f43fe748646cc91370713"),
"Acc_Id": 1,
"Name": "xxxxx",
"Phone": NumberLong("123456789"),
"Email": "xxxxxx@gmail.com"
}
Now, I want to combine Phone
and Email
into contact
and update this collection
db.Acc_Details.findOne()
{
"_id": ObjectId("577f43fe748646cc91370713"),
"Acc_Id": 1,
"Name": "xxxxx",
"Contact": {
"Phone": NumberLong("123456789"),
"Email": "xxxxxx@gmail.com"
}
}
This is what I have tried but I don't know if it's right or not:
db.Acc_Details.aggregate([
{
$project: {
"Contact": {
"$map": {
input: { $literal: ["p1", "p2"] },
as: "p",
in: {
$cond: [
{ $eq: ["$$p", "p1"] },
"$Phone",
"$Email"
]
}
}
}
}
},
{ $unwind: "$Contact" }
])
Result is
{ "_id" : ObjectId("577f43fe748646cc91370713"), "Contact" : NumberLong("12356789") }
{ "_id" : ObjectId("577f43fe748646cc91370713"), "Contact" : "xxxxxx@gmail.com" }
Can someone help me with this?