聚合嵌套数组中的数据

时间:2019-05-20 10:04:32

标签: javascript node.js mongodb mongoose aggregation-framework

我需要有关汇总框架的帮助。 我有一个模型(货币字段可以包含多个对象):

const schema = new mongoose.Schema({
  country: { type: String },
  code: { type: String },
  region: [{
    name: { type: String },
    path: { type: Array },
    city: [{
      name: { type: String },
      path: { type: Array },
      latitude: { type: String },
      longitude: { type: String },
    }],
  }],
  currencies: [{
    code: { type: String },
    name: { type: String },
    symbol: { type: String },
  }],
})

我需要接收所有没有重复的货币。 接收到的数据可以这样查看:

[
  { code: 'string', name: 'sting', symbol: 'string' },
  { code: 'string', name: 'sting', symbol: 'string' },
  ...
]
// or like this:
[
  currencies: [
    { code: 'string', name: 'sting', symbol: 'string' },
    { code: 'string', name: 'sting', symbol: 'string' },
    ...
  ]
]

我尝试创建查询

Geo.aggregate([
  {
    $group: {
       _id: null,
       currencies: { $addToSet: '$currencies' },
    },
  },
])

但是收到重复的数据,并且它有很多嵌套的数组:

[
  {
    "_id": null,
    "currencies": [
      [
        {
          "_id": "5cd9486248989616a411fac5",
          "code": "JPY",
          "name": "Japanese yen",
          "symbol": "¥"
        }
      ],
      [
        {
          "_id": "5cd9491a48989616a411fb47",
          "code": "TRY",
          "name": "Turkish lira",
          "symbol": null
        }
      ],

我尝试以下查询:

Geo.aggregate([
  {
    $addFields: {
      code: '$currencies.code',
      name: '$currencies.name',
      symbol: '$currencies.symbol',
    },
  },
])

但是我收到错误“ TypeError:项目不可迭代”。 我需要一点帮助)

Db数据视图如下:

{
    "_id": {
        "$oid": "5c3334a8871695568817eadf"
    },
    "country": "Singapore",
    "code": "sg",
    "region": [
        {
            "path": [
                "Singapore"
            ],
            "_id": {
                "$oid": "5c3366c63d92ac6e531e05c0"
            },
            "city": [],
            "name": "Central Singapore Community Development Council"
        },
        ....
    ],
    "__v": 0,
    "currencies": [
        {
            "_id": {
                "$oid": "5cd948ec48989616a411fb28"
            },
            "code": "BND",
            "name": "Brunei dollar",
            "symbol": "$"
        },
        {
            "_id": {
                "$oid": "5cd948ec48989616a411fb27"
            },
            "code": "SGD",
            "name": "Singapore dollar",
            "symbol": "$"
        }
    ]
}

2 个答案:

答案 0 :(得分:1)

首先,在汇总管道中,需要展开货币数组,然后按条件将它们分组以获得所需结果。

Geo.aggregate([
 {
   $unwind: '$currencies'
 },
 {
     $group: {
     _id: null,
     currencies: { $addToSet: '$currencies' },
    },
 },
])

有关更多信息,请查看文档here

答案 1 :(得分:0)

db.temp.aggregate([
   {$project : {currencies : 1}}, 
   {$unwind: "$currencies"}, 
   {
      $addFields: {
         currencyHash: {
            $concat : ['$currencies.code', "--", "$currencies.name", "--", "$currencies.symbol"]
         }
      }
   },
   {
      $group: {
         _id: "$currencyHash",
         currency : {
            $first : "$currencies"
         }
      }
   },
   {
       $project: {
          code : "$currency.code",
          name : "$currency.name",
          symbol : "$currency.symbol"
       }
   },
   {
       $project: {
          _id : 0,
          currency : 0
       }
   }
]).pretty()