1
我試圖合併兩個領域 這裏是我收集合併兩個領域爲對象
db.Acc_Details.findOne()
{
"_id": ObjectId("577f43fe748646cc91370713"),
"Acc_Id": 1,
"Name": "xxxxx",
"Phone": NumberLong("123456789"),
"Email": "[email protected]"
}
現在,我想Phone
和Email
合併爲contact
和更新這個集合
db.Acc_Details.findOne()
{
"_id": ObjectId("577f43fe748646cc91370713"),
"Acc_Id": 1,
"Name": "xxxxx",
"Contact": {
"Phone": NumberLong("123456789"),
"Email": "[email protected]"
}
}
這是我所嘗試過的,但我不知道它是否正確:
db.Acc_Details.aggregate([
{
$project: {
"Contact": {
"$map": {
input: { $literal: ["p1", "p2"] },
as: "p",
in: {
$cond: [
{ $eq: ["$$p", "p1"] },
"$Phone",
"$Email"
]
}
}
}
}
},
{ $unwind: "$Contact" }
])
結果是
{ "_id" : ObjectId("577f43fe748646cc91370713"), "Contact" : NumberLong("12356789") }
{ "_id" : ObjectId("577f43fe748646cc91370713"), "Contact" : "[email protected]" }
有人可以幫助我?
但是如何更新表 – raja
@raja添加了更新操作 – chridam