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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用mongodb获得节点Api的结果相关的知识,希望对你有一定的参考价值。

我正在尝试使用芒果聚合的此节点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 
    }
] 
答案

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

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);
  })
})

以上是关于如何使用mongodb获得节点Api的结果的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 REST API 微服务(使用 Express 构建)将 MongoDB 更改流与节点 js 一起使用

如何在地图片段 API v2 布局顶部添加按钮

如何获得 NodeList 的结果?

如何使用 fetch 从一个模块导出从 GET API 获得的响应数据到另一个模块

如何在 MongoDB 中对聚合查询结果进行分页并获得总文档数(Node.js + Mongoose)?

我如何确定 mongodb 是不是返回空结果