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

如何通过对猫鼬进行分组来返回两个匹配条件的计数值?

IT培训 admin 13浏览 0评论

如何通过对猫鼬进行分组来返回两个匹配条件的计数值?

我希望我的输出json如下所示,

[ {
        "_id": "IT",
        "count 1": 1,
        "count 2": 1,

    },
    {
        "_id": "CSE",
        "count1": 1,
        "count2": 2,

    },


]

我在猫鼬中尝试了以下查询

{
            $match: {


                riskstatus: { $in: ["Closed", "Deffered"] },
                riskstatus: { $in: [ "Open"] },
            }
        },

        {

            "$group": {
                "_id": "$phase",
                "count1": { "$sum": 1 },
               "count2": { "$sum": 1 },

            },

        }


我的输入集合为{“ _id”:“ 5df5ca73bb1c4526948e2421”,“评论”:“测试”,“状态”:“打开”,“阶段”:“ CSE”},{“ _id”:“ 5df5ca73bb1c4526948e2422”,“ comment“:” test“,” status“:” closed“,” phase“:” IT“} {” _id“:” 5df5ca73bb1c4526948e2422“,” comment“:” test“,” status“:” Closed“,” phase “:” CSE“} {” _id“:” 5df5ca73bb1c4526948e2422“,”评论“:”测试“,”状态“:”打开“,”阶段“:” IT“}{“ _id”:“ 5df5ca73bb1c4526948e2422”,“评论”:“测试”,“状态”:“打开”,“阶段”:“ CSE”} –

如何在具有上述匹配条件的情况下为同一组检索两个计数值以实现此结果?

我最近尝试过的是

Risk.aggregate([
        { $match: { $or: [ { status: {$in: ["Closed", "Deffered"]} }, { status: {$in: ["Open"] } } ] } },


        {

            "$group": {
                "_id": "$phase",
                " count1": { "$sum": 1 },
                "count2": { "$sum": 1 },

            },

        }
回答如下:

请使用该查询

     db.posts.aggregate(
    {
        $group: {
            _id: { 'id': '$phase', 'status': '$status' },
            count_1: { '$sum': 1 },
        }
    },
    { $project: { '_id': 0, 'depart': '$_id.id', 'status': '$_id.status', 'count_1': 1 } }
    , {
        $group: {
            _id: '$depart',
            data: {
                '$push': {
                    'depart': '$depart',
                    'count_1': {
                        $cond: [{ $eq: ["$status", 'Open'] }, '$count_1', 0]
                    },
                    'count_2': {
                        $cond: [{ $eq: ["$status", 'Closed'] }, '$count_1', 0]
                    }
                    ,
                    'count_3': {
                        $cond: [{ $eq: ["$status", 'Deffered'] }, '$count_1', 0]
                    }

                },
            }
        }
    },
      { $project: 
      {'name':'$_id','_id':0, 
      openCount: { $max: "$data.count_1"}, 
      Othercount: {  
          $add:[ 
              {$max: "$data.count_2"},
              {$max: "$data.count_3"}]

      } 

      }}

)

如何通过对猫鼬进行分组来返回两个匹配条件的计数值?

我希望我的输出json如下所示,

[ {
        "_id": "IT",
        "count 1": 1,
        "count 2": 1,

    },
    {
        "_id": "CSE",
        "count1": 1,
        "count2": 2,

    },


]

我在猫鼬中尝试了以下查询

{
            $match: {


                riskstatus: { $in: ["Closed", "Deffered"] },
                riskstatus: { $in: [ "Open"] },
            }
        },

        {

            "$group": {
                "_id": "$phase",
                "count1": { "$sum": 1 },
               "count2": { "$sum": 1 },

            },

        }


我的输入集合为{“ _id”:“ 5df5ca73bb1c4526948e2421”,“评论”:“测试”,“状态”:“打开”,“阶段”:“ CSE”},{“ _id”:“ 5df5ca73bb1c4526948e2422”,“ comment“:” test“,” status“:” closed“,” phase“:” IT“} {” _id“:” 5df5ca73bb1c4526948e2422“,” comment“:” test“,” status“:” Closed“,” phase “:” CSE“} {” _id“:” 5df5ca73bb1c4526948e2422“,”评论“:”测试“,”状态“:”打开“,”阶段“:” IT“}{“ _id”:“ 5df5ca73bb1c4526948e2422”,“评论”:“测试”,“状态”:“打开”,“阶段”:“ CSE”} –

如何在具有上述匹配条件的情况下为同一组检索两个计数值以实现此结果?

我最近尝试过的是

Risk.aggregate([
        { $match: { $or: [ { status: {$in: ["Closed", "Deffered"]} }, { status: {$in: ["Open"] } } ] } },


        {

            "$group": {
                "_id": "$phase",
                " count1": { "$sum": 1 },
                "count2": { "$sum": 1 },

            },

        }
回答如下:

请使用该查询

     db.posts.aggregate(
    {
        $group: {
            _id: { 'id': '$phase', 'status': '$status' },
            count_1: { '$sum': 1 },
        }
    },
    { $project: { '_id': 0, 'depart': '$_id.id', 'status': '$_id.status', 'count_1': 1 } }
    , {
        $group: {
            _id: '$depart',
            data: {
                '$push': {
                    'depart': '$depart',
                    'count_1': {
                        $cond: [{ $eq: ["$status", 'Open'] }, '$count_1', 0]
                    },
                    'count_2': {
                        $cond: [{ $eq: ["$status", 'Closed'] }, '$count_1', 0]
                    }
                    ,
                    'count_3': {
                        $cond: [{ $eq: ["$status", 'Deffered'] }, '$count_1', 0]
                    }

                },
            }
        }
    },
      { $project: 
      {'name':'$_id','_id':0, 
      openCount: { $max: "$data.count_1"}, 
      Othercount: {  
          $add:[ 
              {$max: "$data.count_2"},
              {$max: "$data.count_3"}]

      } 

      }}

)
发布评论

评论列表 (0)

  1. 暂无评论