簡體   English   中英

聚合 Mongodb 3.4

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM