如何使用mongodb获得节点Api的结果

问题描述 投票:-1回答:1

我正在尝试使用芒果聚合的此节点API,但没有得到一些意外的令牌问题。一些提出意外的令牌问题

      const monthsEnum = {
        "_id": "year",
        "1": "January",
        "2": "February",
        "3": "March",
        "4": "April",
        "5": "May",
        "6": "June",
        "7": "July",
        "8": "August",
        "9": "September",
        "10": "October",
        "11": "November",
        "12": "December"
    };

    app.get('/polute', function (req, res) {
      Air_pollution.aggregate([
      { "$match": {
          "CREATE_DATE": {
              "$lte": new Date(),
              "$gte": new Date(new Date().setDate(new Date().getDate()-120))
          }
      } },
      { "$group": {
          "_id": {
              "month": { "$month": "$CREATE_DATE" },
              "year": { "$year": "$CREATE_DATE" }
          },
          "avgofozone": { "$avg": "$OZONE" }
      } },
      { "$group": {
          "_id": "$year",
          "avgs": {
              "$push": {
                  "k": { "$substr": ["$month", 0, -1 ] },
                  "v": "$avgofozone"
              }
          }
      } },
      { "$replaceRoot": {
          "newRoot": {
              "$mergeObjects": [
                  { "$arrayToObject": "$avgs" },
                  "$$ROOT"
               ]
          }
      } },
      { "$project": { "avgs": 0 } }
    ], (err, Air_pollution) => {
      console.log("naresh:" +JSON.stringify(Air_pollution));
      const polute = Object.keys(Air_pollution).reduce((p, c) => ({...p, monthsEnum[c]: Air_pollution[c]}), {});
      res.json(Air_pollution);
    }), 
expected output:
[
    { 
        "zone_type": "avgofozone", 
        "year": 2018, 
        "February": 21.07777777777778, 
        "March": 17.8, 
        "January": 17.8 
    }
] 
javascript node.js mongodb mongoose mongodb-query
1个回答
0
投票

定义对象时,需要将变量属性名称完全包含在[]中。更改

const polute = Object.keys(Air_pollution)
  .reduce((p, c) => ({
    ...p,
    monthsEnum[c]: Air_pollution[c]
  }), {});

const polute = Object.keys(Air_pollution)
  .reduce((p, c) => ({
    ...p,
    [monthsEnum[c]]: Air_pollution[c]
  }), {});

我还建议提前定义大型数组,这样你就可以同时看到功能的内容,有点像这样:

const monthsEnum = {
  "_id": "year",
  "1": "January",
  "2": "February",
  "3": "March",
  "4": "April",
  "5": "May",
  "6": "June",
  "7": "July",
  "8": "August",
  "9": "September",
  "10": "October",
  "11": "November",
  "12": "December"
};

const arr = [{
    "$match": {
      "CREATE_DATE": {
        "$lte": new Date(),
        "$gte": new Date(new Date().setDate(new Date().getDate() - 120))
      }
    }
  },
  {
    "$group": {
      "_id": {
        "month": {
          "$month": "$CREATE_DATE"
        },
        "year": {
          "$year": "$CREATE_DATE"
        }
      },
      "avgofozone": {
        "$avg": "$OZONE"
      }
    }
  },
  {
    "$group": {
      "_id": "$year",
      "avgs": {
        "$push": {
          "k": {
            "$substr": ["$month", 0, -1]
          },
          "v": "$avgofozone"
        }
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$mergeObjects": [{
            "$arrayToObject": "$avgs"
          },
          "$$ROOT"
        ]
      }
    }
  },
  {
    "$project": {
      "avgs": 0
    }
  }
];

app.get('/polute', function(req, res) {
  Air_pollution.aggregate(arr, (err, Air_pollution) => {
    console.log("naresh:" + JSON.stringify(Air_pollution));
    const polute = Object.keys(Air_pollution)
      .reduce((p, c) => ({
        ...p,
        [monthsEnum[c]]: Air_pollution[c]
      }), {});
    res.json(Air_pollution);
  })
})
© www.soinside.com 2019 - 2024. All rights reserved.