简体   繁体   中英

Grouping and counting Operations in MongoDB

I have two collections,which look like as follows

Ticket_master

{
    "_id" : ObjectId("5e70ed53de0f7507d4da8dc2"),
    "TICKET_NO" : 1000,
    "DESCRIPTION" : "<p>dsdsd</p>",
    "ASSIGNED_TO" : ObjectId("5e78f2b2c0e09128e81db475"),
    "RESOLVED_BY" : ObjectId("5e6f1c5b8451307f782d0992")
}

/* 2 */
{
    "_id" : ObjectId("5e70f19c47df9479502f7933"),
    "TICKET_NO" : 1001,
    "DESCRIPTION" : "<p>vcvcv</p>",
    "ASSIGNED_TO" : ObjectId("5e706e914587054254c02085"),
    "RESOLVED_BY" : ObjectId("5e6f1c5b8451307f782d0992")
}

/* 3 */
{
    "_id" : ObjectId("5e70f4fa47df9479502f7937"),
    "TICKET_NO" : 1002,
    "DESCRIPTION" : "<p>xcxc</p>",
    "ASSIGNED_TO" : ObjectId("5e706e914587054254c02085"),
    "RESOLVED_BY" : ObjectId("5e706e914587054254c02085")
}

Agent_master

{
    "_id" : ObjectId("5e6f1c5b8451307f782d0992"),
    "NAME" : "A1",
}

/* 2 */
{
    "_id" : ObjectId("5e706e914587054254c02085"),
    "NAME" : "A2",
}

/* 3 */
{
    "_id" : ObjectId("5e78f2b2c0e09128e81db475"),
    "NAME" : "A3",
}

I need to perform various database operation like grouping and counting for getting the expected result,which is mentioned below

Expected Output :

[
    {
         NAME : 'A1',
         COUNT:2,
    },
    {
        NAME : 'A2',
        COUNT:1,
    },
    {
        NAME : 'A3',
        COUNT:0,
    }

]

I want to find the number of tickets resolved by each agents with name of the agents. RESOLVED_BY refers to the agent id who resolve the ticket.

I hope you understand what I mean! Thank you

Tried with following Query :

db.getCollection('ticket_masters').aggregate([
    {"$group" : {_id:"$RESOLVED_BY", count:{$sum:1}}}
    ])

Actual Output :

/* 1 */
{
    "_id" : ObjectId("5e78f2ddc0e09128e81db47a"),
    "count" : 1.0
}

/* 2 */
{
    "_id" : ObjectId("5e6f1c5b8451307f782d0992"),
    "count" : 4.0
}

/* 3 */
{
    "_id" : null,
    "count" : 6.0
}

You can try below query :

db.Ticket_master.aggregate([
  /** Get agents info who resolved tickets */
  {
    $lookup: {
      from: "Agent_master",
      localField: "RESOLVED_BY",
      foreignField: "_id",
      as: "agent_docs"
    }
  },
  /** As agent_docs is an array unwind into make it into objects */
  {
    $unwind: "$agent_docs"
  },
  /** Group on 'NAME' & count no.of occurrences of each NAME */
  {
    $group: {
      _id: "$agent_docs.NAME",
      COUNT: {
        $sum: 1
      }
    }
  },
  /** Transform fields into required format */
  {
    $project: {
      _id: 0,
      NAME: "$_id",
      COUNT: 1
    }
  }
])

Test : MongoDB-Playground

Here is answer

db.Agent_master.aggregate([
    {
        $lookup: {
            "from": "Ticket_master",
            "localField": "_id",
            "foreignField": "RESOLVED_BY",
            "as": "tickets"
        }
    },
    {
        $project:  {
            Name: '$Name',
            Count: { $size: '$tickets'}
        }
    }
])

You have to use $lookup to jon two collections. Since you only need count so it can be done by $project only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM