![](/img/trans.png)
[英]Concatenate array elements in MongoDB 3.4 with Aggregation Framework
[英]Aggregation Mongodb 3.4
我有一個像這樣的機智文檔集合。 我想更改子文檔中的值
{ "cities" :[
{
"city" : "London",
"country" : "United Kingdom"
},
{
"city" : "New York",
"country" : "United States"
}
{
"city" : "San Francisco",
"country" : "United States"
}]
}
如果城市是San Francisco
那么San Francisco
如果城市是New york
那么紐約
否則為null
我使用了$switch
但不起作用。 我正在使用 MongoDB v3.4
。
試試這個:
db.collection.aggregate([
{
$project: {
cities: {
$map: {
input: "$cities",
in: {
country: "$$this.country",
city: {
$switch: {
branches: [
{
case: {
$eq: [ "$$this.city","San Francisco"]
},
then: "Sf"
},
{
case: {
$eq: [ "$$this.city", "New York" ]
},
then: "NY"
}
],
default: null
}
}
}
}
}
}
}
//Uncomment this stage if aggregation result meets your requirements
//,{$out: "collection"}
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.