最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

是否要按月查看注册用户的数量?

IT培训 admin 9浏览 0评论

是否要按月查看注册用户的数量?

我正在编写查询,该查询将提供每月注册用户的数量。特定月份完成了多少注册。该月将按从1月到12月的升序排列。请帮助我解决此问题。一个月应该用单词而不是数字。我的方法如下:-

User.aggregate([{
    $match: {
      createdAt: {
        $gte: new Date("2016-01-01")
      } 
    } 
  }, { 
    $group: {
      _id: { 

        "month": { "$month": "$createdAt" },

      },
      count:{$sum: 1}
    }
  },{"$sort": {"createdAt": 1}}]).exec(function(err,data){
    if (err) {
      console.log('Error Fetching model');
      res.status(500).send();
    } else {
      res.send(data);
    }
  });

实际输出:-

{
    "_id" : {
        "month" : 4
    },
    "count" : 1.0
}

/* 2 */
{
    "_id" : {
        "month" : 5
    },
    "count" : 8.0
}

/* 3 */
{
    "_id" : {
        "month" : 2
    },
    "count" : 1.0
}

/* 4 */
{
    "_id" : {
        "month" : 3
    },
    "count" : 1.0
}

//预期输出

 {
        "_id" : {
            "month" : "February"
        },
        "count" : 1.0
    }


    {
        "_id" : {
            "month" : "March"
        },
        "count" : 1.0
    }

    {
        "_id" : {
            "month" : "April"
        },
        "count" : 1.0
    }

    {
        "_id" : {
            "month" : "May"
        },
        "count" : 8.0
    }
回答如下:看起来像$switch的一个好用例:

db.collection.aggregate([ // your aggregation stages { $addFields: { "_id.month": { $switch: { branches: [ { case: { $eq: [ "$_id.month", 1 ] }, then: "January" }, { case: { $eq: [ "$_id.month", 2 ] }, then: "February" }, { case: { $eq: [ "$_id.month", 3 ] }, then: "March" }, { case: { $eq: [ "$_id.month", 4 ] }, then: "April" } // ... ], default: "December" } } } } ])

Mongo Playground

是否要按月查看注册用户的数量?

我正在编写查询,该查询将提供每月注册用户的数量。特定月份完成了多少注册。该月将按从1月到12月的升序排列。请帮助我解决此问题。一个月应该用单词而不是数字。我的方法如下:-

User.aggregate([{
    $match: {
      createdAt: {
        $gte: new Date("2016-01-01")
      } 
    } 
  }, { 
    $group: {
      _id: { 

        "month": { "$month": "$createdAt" },

      },
      count:{$sum: 1}
    }
  },{"$sort": {"createdAt": 1}}]).exec(function(err,data){
    if (err) {
      console.log('Error Fetching model');
      res.status(500).send();
    } else {
      res.send(data);
    }
  });

实际输出:-

{
    "_id" : {
        "month" : 4
    },
    "count" : 1.0
}

/* 2 */
{
    "_id" : {
        "month" : 5
    },
    "count" : 8.0
}

/* 3 */
{
    "_id" : {
        "month" : 2
    },
    "count" : 1.0
}

/* 4 */
{
    "_id" : {
        "month" : 3
    },
    "count" : 1.0
}

//预期输出

 {
        "_id" : {
            "month" : "February"
        },
        "count" : 1.0
    }


    {
        "_id" : {
            "month" : "March"
        },
        "count" : 1.0
    }

    {
        "_id" : {
            "month" : "April"
        },
        "count" : 1.0
    }

    {
        "_id" : {
            "month" : "May"
        },
        "count" : 8.0
    }
回答如下:看起来像$switch的一个好用例:

db.collection.aggregate([ // your aggregation stages { $addFields: { "_id.month": { $switch: { branches: [ { case: { $eq: [ "$_id.month", 1 ] }, then: "January" }, { case: { $eq: [ "$_id.month", 2 ] }, then: "February" }, { case: { $eq: [ "$_id.month", 3 ] }, then: "March" }, { case: { $eq: [ "$_id.month", 4 ] }, then: "April" } // ... ], default: "December" } } } } ])

Mongo Playground
发布评论

评论列表 (0)

  1. 暂无评论