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

时间:2018-04-15 03:48:15

标签: javascript node.js mongodb mongoose mongodb-query

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

map(c("CB1", "LB2", "CW3", "WC1", "LW20"), ~ df %>%
    mutate(
        !!(paste0(.x, "_score", collapse = "")) :=
            UQ(rlang::sym(.x)) == UQ(rlang::sym(paste0(.x, "_ans", collapse = ""))) )) %>%
    reduce(., left_join)
#  CB1 LB2 CW3 WC1 LW20 CB1_ans LB2_ans CW3_ans WC1_ans LW20_ans CB1_score
#1   9   5   5   9    9       5       7       8       4        9     FALSE
#2   9   7   7   9    2       4       8       1       4        8     FALSE
#3   3   2   9   2    9       9       4       2       1        6     FALSE
#4   8   6   3   5    9       5       7       9       9        9     FALSE
#5   6   7   5   6    1       8       1       6       4        6     FALSE
#  LB2_score CW3_score WC1_score LW20_score
#1     FALSE     FALSE     FALSE       TRUE
#2     FALSE     FALSE     FALSE      FALSE
#3     FALSE     FALSE     FALSE      FALSE
#4     FALSE     FALSE     FALSE       TRUE
#5     FALSE     FALSE     FALSE      FALSE

1 个答案:

答案 0 :(得分: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);
  })
})