node js mongo aggregate不起作用

时间:2018-02-01 14:34:57

标签: node.js mongodb aggregate-functions

您好我正在将Node js与Async结合使用。我试图用mongodb的aggegrate声明。我已成功在MongoDB shell中执行此语句,但它不适用于Node。

Mongo Shell中的声明是:

db.stgOmniTracker.aggregate([{ $match: {State:"Solved" } },{ $group: {_id: {lastChangeWeek: "$lastChangeWeek", lastChangeYear: "$lastChangeYear"}totalCount: { $sum: "$count" }} })

我在节点中使用的语句是:

var  request = require("request")
    ,async = require('async')
    ,mongo = require('mongodb')
    ,d3 = require('d3')
    ,uri = 'mongodb://localhost:27017/commevents'
    ,moment = require('moment')
    ,underscore = require('underscore')
    ,actWeek = actualWeek()
    ,actMonth =  actualMonth()
    ,ftlrGroup = []
    ,fltrState = []
    ,compareWordGroup = null
    ,compareWordState = null
    ,locals = {}

exports.getTickets = function (req, res, next) {

    console.info('-------------------Get Tickets --------------------------------------------------------')

    mongo.connect(uri, function (err, db) {
        console.info('MONGODB START CHECK COLLECTIONS')
        var tasks = [  
            // Load stgOmniTracker - prepare measureSet rawtotCreatedPerWeek
            function (callback) {
                db.collection('stgOmniTracker').aggregate([ { $group: {_id: {creationWeek: "$creationWeek", creationYear: "$creationYear"}totalCount: { $sum: "$count" }} }]).toArray(function (err, rawtotCreatedPerWeek) {
                    if (err) return callback(err);
                    locals.rawtotCreatedPerWeek = rawtotCreatedPerWeek;
                    callback();
                });
            }]
        console.info('--------------- START ASYNC ------------------------')
        async.parallel(tasks, function (err) {
            if (err) return next(err);
            var rawtotCreatedPerWeek = locals.rawtotCreatedPerWeek
            db.close()
        })

我从节点返回以下错误:

/home/erik/git/WisdomAsAService/views/Dashboard/getTickets.js:xx
                    db.collection('stgOmniTracker').aggregate([ { $group: {_id: {creationWeek: "$creationWeek", creationYear: "$creationYear"}totalCount: { $sum: "$count" }} }]).toArray(function (err, rawtotCreatedPerWeek) {
                                                                                                                                              ^^^^^^^^^^

    SyntaxError: Unexpected identifier

你知道我怎么解决这个问题吗?

曼尼谢谢,

埃里克

1 个答案:

答案 0 :(得分:1)

您的查询无效。在我看来,你基本上缺少一个逗号。

SyntaxError: Unexpected identifier通常意味着您在不合适的地方有一个令牌,javascript无法理解。解决这些错误的最佳(或最简单)方法是使用静态代码分析器,如eslint,flow或sonar qube。

这些工具也可以与流行的编辑器很好地集成。