【问题标题】:how to get result with node Api with mongodb如何使用 mongodb 使用节点 Api 获取结果
【发布时间】:2018-09-25 01:41:52
【问题描述】:

我正在尝试使用芒果聚合的这个节点 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】:

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

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

    【讨论】:

    • @CertainPerformance 我正在尝试这个,但没有任何建议
    • 由于语法无效,您遇到了问题,这就是您正确编写该行的方式。 but not working 不告诉任何人任何事情
    • @CertainPerformance 错误抛出 Unexpected token const 请任何建议我
    • 听起来您复制粘贴不正确并覆盖(或覆盖)了您不应该拥有的行。如果您之前没有收到来自 const polute 的错误,那么您现在不应该收到错误,除非出现粘贴错误。
    • @CertainPerformance { "$replaceRoot": { "newRoot": { "$mergeObjects": [{ "$arrayToObject": "$avgs" }, "$$ROOT" ] } } } 是不替换值可能是一些错误,请查看一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多