node.js/快速路由中的猫鼬组查询 [英] Mongoose group query in node.js / express route

查看:154
本文介绍了node.js/快速路由中的猫鼬组查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Robomongo中执行了以下查询,该查询按预期工作:

Hello i have the following query that executed in Robomongo works as expected:

db.av.group(
{
 key: { roomId: 1},
 cond: { dateOfDay: { $gte: new Date('12/01/2014'), $lt: new Date('12/30/2014') } },
 reduce: function( curr, result ) {
            result.total += curr.price;
            result.count++;
         },
 initial: { total : 0,
     count : 0
     },
     finalize: function(result) {
              result.avg = Math.round(result.total / result.count);
          }

  }
)

在快速应用中实施该操作后:

After implemented that in a express app :

 app.get('/api/checkAv/:checkIn/:checkOut', function(req, res) {
        var checkIn = req.params.checkIn,
            checkOut = req.params.checkOut,
            roomType = req.params.roomType;

    model.Av.group(
    {
     key: { roomId: 1},
     cond: { dateOfDay: { $gte: new Date('12/01/2014'), $lt: new Date('12/30/2014') } },
     reduce: function( curr, result ) {
                result.total += curr.price;
                result.count++;
             },
     initial: { total : 0,
         count : 0
         },
         finalize: function(result) {
                  result.avg = Math.round(result.total / result.count);
              }

    }

    ).exec(function(err, av) {
        if (err)
            res.send(err);

        res.json(av);
    });
});

我进入控制台:

TypeError:对象函数模型(文档,字段,skipId){if(!(this 模型的实例)) 返回新模型(doc,fields,skipId); Model.call(this,doc,fields,skipId); }处没有方法'group' /home/www/domain.com/api/routes/routes.js:58:18在Layer.handle [as handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
在下一个 (/home/www/domain.com/node_modules/express/lib/router/route.js:100:13) 在Route.dispatch (/home/www/domain.com/node_modules/express/lib/router/route.js:81:3)
在Layer.handle [作为handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
在 /home/www/brasovapartments.com/node_modules/express/lib/router/index.js:234:24 在param (/home/www/domain.com/node_modules/express/lib/router/index.js:331:14) 在param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) 在param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) 在Function.proto.process_params (/home/www/domain.com/node_modules/express/lib/router/index.js:391:3)

TypeError: Object function model(doc, fields, skipId) { if (!(this instanceof model)) return new model(doc, fields, skipId); Model.call(this, doc, fields, skipId); } has no method 'group' at /home/www/domain.com/api/routes/routes.js:58:18 at Layer.handle [as handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
at next (/home/www/domain.com/node_modules/express/lib/router/route.js:100:13) at Route.dispatch (/home/www/domain.com/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
at /home/www/brasovapartments.com/node_modules/express/lib/router/index.js:234:24 at param (/home/www/domain.com/node_modules/express/lib/router/index.js:331:14) at param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) at param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) at Function.proto.process_params (/home/www/domain.com/node_modules/express/lib/router/index.js:391:3)

现在阅读一遍,我看到有些人定义了一个group属性,但是不确定在Mongoose模式中如何以及在何处..? 您能给我一个提示吗,我在这里遇到什么问题?

now reading around i saw some people defined a group property but not sure how and where .. in the Mongoose schema? Can you gave me a hint what's my problem over-here?

推荐答案

您可以使用aggregate这样的方法来完成此操作:

You can do this with aggregate using something like:

model.Av.aggregate([
    {$match: {dateOfDay: {$gte: new Date('12/01/2014'), $lt:new Date('12/30/2014')}}},
    {$group: {
        _id: '$roomId',
        total: {$sum: '$price'},
        count: {$sum: 1},
        avg: {$avg: '$price'}
    }}
], function (err, result) {...});

如果不需要它们,可以省略totalcount字段,作为

You can omit the total and count fields if you don't need them as the $avg operator lets you calculate the average price directly.

这篇关于node.js/快速路由中的猫鼬组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆