db.getCollection("user").aggregate([
{ $match : { fname : "test1" } },
{ $addFields: { email_address : "[email protected]" } },
{ $match : { fname : "test2" } },
{ $addFields: { email_address : "[email protected]" } }
])
我只想根據 fname 值添加電子郵件地址。此代碼僅在沒有 test2 時才有效,因此可以使用一次但不要重復。有什么方法可以重復匹配和添加欄位嗎?
uj5u.com熱心網友回復:
您可以$switch用于您的情況。
db.collection.aggregate([
{
"$addFields": {
"email_address": {
"$switch": {
"branches": [
{
"case": {
$eq: [
"$fname",
"test1"
]
},
"then": "[email protected]"
},
{
"case": {
$eq: [
"$fname",
"test2"
]
},
"then": "[email protected]"
}
]
}
}
}
}
])
這是Mongo 游樂場供您參考。
uj5u.com熱心網友回復:
在一般情況下(如果適用):
db.getCollection("user").aggregate([
{ $addFields: { email_address : {$concat: ["$fname", "@gmail.com"]} }}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/385507.html
