【问题标题】:Bluebird promisify mongoose group by蓝鸟承诺猫鼬组由
【发布时间】:2015-11-19 17:40:08
【问题描述】:

我有一个猫鼬模型

'use strict';

var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var Schema = mongoose.Schema;

function toLower (v) {
  return v.toLowerCase();
}

var Signup = new Schema({
  name: { type: String, required: true },
  email: { type: String, unique: true, required: true, set: toLower },
  position: { type: String },
  area: String,
  companySize: Number,
  created: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Signup', Signup);

我想按创建(日期格式为天)分组并计算 _id 的数量。

我是猫鼬和蓝鸟的新手……谁能给我举个例子?谢谢!!!

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我设法得到了我想要的:

    SignupModel
      .aggregate([
        {
          $group: {
            _id: { $week: "$created" },
            "count": { "$sum": 1 }
          }
        }
      ])
      .execAsync()
      .then(responseWithResult(res))
      .catch(handleError(res));
    

    => 按创建(周)分组并求和

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 2016-06-04
      • 1970-01-01
      • 2016-11-21
      • 2016-09-23
      • 2016-06-10
      • 2015-03-05
      • 2015-03-06
      • 2016-05-25
      相关资源
      最近更新 更多