简体   繁体   中英

Mongodb Count on multiple sub arrays

Calculate All the number of agreements, contacts and the group by it on company name. Note. Agreements and Contacts are array of objects

This is a sample document of my data:

{
    "_id" : ObjectId("57c7bd8f51666ac615bc837d"),
    "ITBCompanyId" : 2296,
    "updatedAt" : ISODate("2016-09-01T05:38:02.843Z"),
    "createdAt" : ISODate("2016-09-01T05:33:03.604Z"),
    "identifier" : "SOU10",
    "name" : "Source One Rehabilitation",
    "addressLine2" : null,
    "city" : "Plano",
    "state" : "TX",
    "zip" : "75024",
    "faxNumber" : "",
    "territoryId" : "11",
    "marketId" : null,
    "accountNumber" : "Source One Rehabilitation",
    "dateAcquired" : ISODate("2014-09-15T04:00:00.000Z"),
    "parentCompany" : null,
    "annualRevenue" : null,
    "numberOfEmployees" : null,
    "ownershipType" : null,
    "deletedBy" : null,
    "orgId" : "1",
    "deletedAt" : null,
    "_info" : {
        "lastUpdated" : ISODate("2016-09-01T05:12:32.000Z"),
        "updatedBy" : "LabTechInt"
    },
    "whois" : [],
    "configuration" : [],
    "contact" : [ 
        {
            "mobileGuid" : "6435d4c0-12fa-41d1-9db0-e4f9b1b4e7b2",
            "presence" : null,
            "gender" : null,
            "title" : null,
            "country" : null,
            "zip" : "75024",
            "state" : "TX",
            "city" : "Plano",
            "addressLine2" : null,
            "lastName" : null,
            "firstName" : "Dee Williams",
            "id" : 821,
            "_id" : ObjectId("57c7beba51666ac615bc8576"),
            "communicationItems" : [],
            "customFields" : null,
            "relationship" : {
                "name" : null,
                "id" : 0
            }
        }, 
        {
            "id" : 829,
            "firstName" : "Jesy",
            "lastName" : "Leon",
            "addressLine2" : null,
            "city" : "Plano",
            "state" : "TX",
            "zip" : "75024",
            "country" : null,
            "title" : null,
            "gender" : null,
            "presence" : null,
            "mobileGuid" : "2b0754c1-c256-4063-9cea-c40f9ca77084",

            "_id" : ObjectId("57c7beba51666ac615bc857d"),
            "communicationItems" : [ 
                {
                    "communicationType" : "Email",
                    "defaultFlag" : true,
                    "extension" : null,
                    "type" : {
                        "_info" : null,
                        "name" : "Email",
                        "id" : 1
                    },
                    "id" : 45587
                }, 
                {
                    "communicationType" : "Phone",
                    "defaultFlag" : true,
                    "extension" : null,
                    "value" : "9728012190",
                    "type" : {
                        "_info" : null,
                        "name" : "Direct",
                        "id" : 2
                    },
                    "id" : 45590
                }
            ],
            "customFields" : null,
            "company" : {
                "id" : "19390",
                "name" : "Source One Rehabilitation",
            },
            "relationship" : {
                "id" : 0,
                "name" : null
            }
        }, 
        {
            "id" : 850,
            "firstName" : "Dr.",
            "lastName" : "Gabriel",
            "addressLine2" : null,
            "city" : "Plano",
            "state" : "TX",
            "zip" : "75024",
            "country" : null,
            "title" : null,
            "gender" : null,
            "presence" : null,
            "mobileGuid" : "8c833a31-369d-4499-87cb-b5598826e653",
            "_id" : ObjectId("57c7beba51666ac615bc8592"),
            "communicationItems" : [ 
                {
                    "communicationType" : "Email",
                    "defaultFlag" : true,
                    "extension" : null,
                    "type" : {
                        "_info" : null,
                        "name" : "Email",
                        "id" : 1
                    },
                    "id" : 45627
                }, 
                {
                    "communicationType" : "Phone",
                    "defaultFlag" : true,
                    "extension" : null,
                    "value" : "9728012190",
                    "type" : {
                        "_info" : null,
                        "name" : "Direct",
                        "id" : 2
                    },
                    "id" : 45628
                }
            ],
            "customFields" : null,
            "relationship" : {
                "id" : 0,
                "name" : null
            }
        }
    ],
    "agreement" : [ 
        {
            "periodType" : null,
            "billAmount" : "0",
            "billTermsId" : 12,
            "billOneTimeFlag" : false,
            "billCycleId" : "2",
            "expiredDays" : "0",
            "coverAgreementExpense" : false,
            "coverAgreementProduct" : false,
            "coverAgreementTime" : true,
            "oneTimeFlag" : false,
            "applicationUnlimitedFlag" : true,
            "applicationCycle" : "CalendarMonth",
            "applicationLimit" : "0",
            "applicationUnits" : "Hours",
            "internalNotes" : "",
            "workOrder" : "",
            "slaId" : "2",
            "reasonCancelled" : "",
            "dateCancelled" : null,
            "cancelledFlag" : false,
            "noEndingDateFlag" : false,
            "endDate" : "2016-01-31T05:00:00Z",
            "startDate" : "2015-02-01T05:00:00Z",
            "businessUnitId" : 2,
            "locationId" : "11",
            "customerPO" : "",
            "parentAgreementId" : null,
            "name" : "Complete Managed Services Agreement",
            "id" : "109",
            "_id" : ObjectId("57c7bd9051666ac615bc83e3"),
            "_info" : {
                "updatedBy" : "Ali            ",
                "lastUpdated" : ISODate("2016-05-12T20:55:21.000Z")
            },
            "workType" : {
                "_info" : null,
                "name" : "Remote",
                "id" : "3"
            },
            "workRole" : {
                "_info" : null,
                "name" : "Support Engineer",
                "id" : "5"
            },
            "contact" : {
                "name" : "Heather B",
                "id" : "539"
            },
            "agreementType" : {
                "name" : "Complete Managed Services",
                "id" : "19"
            }
        }, 
        {
            "id" : "121",
            "name" : "Complete Managed Services: IMV",
            "parentAgreementId" : null,
            "customerPO" : "",
            "locationId" : "11",
            "businessUnitId" : 2,
            "startDate" : "2016-03-01T05:00:00Z",
            "endDate" : null,
            "noEndingDateFlag" : true,
            "cancelledFlag" : false,
            "dateCancelled" : null,
            "reasonCancelled" : "",
            "slaId" : "2",
            "workOrder" : "",
            "internalNotes" : "",
            "applicationUnits" : "Hours",
            "applicationLimit" : "0",
            "applicationCycle" : "CalendarMonth",
            "applicationUnlimitedFlag" : true,
            "oneTimeFlag" : false,
            "coverAgreementTime" : true,
            "coverAgreementProduct" : false,
            "coverAgreementExpense" : false,
            "expiredDays" : "0",
            "billCycleId" : "2",
            "billOneTimeFlag" : false,
            "billTermsId" : 12,
            "billAmount" : "0",
            "periodType" : null,
            "_id" : ObjectId("57c7bd9051666ac615bc83fa"),
            "_info" : {
                "lastUpdated" : ISODate("2016-05-12T22:11:59.000Z"),
                "updatedBy" : "Ali            "
            },
            "workType" : {
                "id" : "3",
                "name" : "Remote",
                "_info" : null
            },
            "workRole" : {
                "id" : "5",
                "name" : "Support Engineer",
                "_info" : null
            },
            "contact" : {
                "id" : "810",
                "name" : "Jarrod Rogers"
            },
            "agreementType" : {
                "id" : "19",
                "name" : "Complete Managed Services"
            }
        }, 
        {
            "id" : "122",
            "name" : "Complete Managed Servicess: FTW",
            "parentAgreementId" : null,
            "customerPO" : "",
            "locationId" : "11",
            "businessUnitId" : 2,
            "startDate" : "2016-03-01T05:00:00Z",
            "endDate" : null,
            "noEndingDateFlag" : true,
            "cancelledFlag" : false,
            "dateCancelled" : null,
            "reasonCancelled" : "",
            "slaId" : "2",
            "workOrder" : "",
            "internalNotes" : "",
            "applicationUnits" : "Hours",
            "applicationLimit" : "0",
            "applicationCycle" : "CalendarMonth",
            "applicationUnlimitedFlag" : true,
            "oneTimeFlag" : false,
            "coverAgreementTime" : true,
            "coverAgreementProduct" : false,
            "coverAgreementExpense" : false,
            "expiredDays" : "0",
            "billCycleId" : "2",
            "billOneTimeFlag" : false,
            "billTermsId" : 12,
            "billAmount" : "0",
            "periodType" : null,
            "_id" : ObjectId("57c7bd9051666ac615bc83fd"),
            "_info" : {
                "lastUpdated" : ISODate("2016-05-12T22:14:29.000Z"),
                "updatedBy" : "Ali            "
            },
            "workType" : {
                "id" : "3",
                "name" : "Remote",
                "_info" : null
            },
            "workRole" : {
                "id" : "5",
                "name" : "Support Engineer",
                "_info" : null
            },
            "contact" : {
                "id" : "810",
                "name" : "Jarrod Rogers"
            },
            "agreementType" : {
                "id" : "19",
                "name" : "Complete Managed Services"
            }
        }, 
        {
            "id" : "123",
            "name" : "Complete Managed Servicess: SC",
            "parentAgreementId" : null,
            "customerPO" : "",
            "locationId" : "11",
            "businessUnitId" : 2,
            "startDate" : "2016-03-01T05:00:00Z",
            "endDate" : null,
            "noEndingDateFlag" : true,
            "cancelledFlag" : false,
            "dateCancelled" : null,
            "reasonCancelled" : "",
            "slaId" : "2",
            "workOrder" : "",
            "internalNotes" : "",
            "applicationUnits" : "Hours",
            "applicationLimit" : "0",
            "applicationCycle" : "CalendarMonth",
            "applicationUnlimitedFlag" : true,
            "oneTimeFlag" : false,
            "coverAgreementTime" : true,
            "coverAgreementProduct" : false,
            "coverAgreementExpense" : false,
            "expiredDays" : "0",
            "billCycleId" : "2",
            "billOneTimeFlag" : false,
            "billTermsId" : 12,
            "billAmount" : "0",
            "periodType" : null,
            "_id" : ObjectId("57c7bd9051666ac615bc83ff"),
            "_info" : {
                "lastUpdated" : ISODate("2016-05-12T22:13:59.000Z"),
                "updatedBy" : "Ali            "
            },
            "workType" : {
                "id" : "3",
                "name" : "Remote",
                "_info" : null
            },
            "workRole" : {
                "id" : "5",
                "name" : "Support Engineer",
                "_info" : null
            },
            "contact" : {
                "id" : "810",
                "name" : "Jarrod Rogers"
            },
            "agreementType" : {
                "id" : "19",
                "name" : "Complete Managed Services"
            }
        }, 
        {
            "id" : "124",
            "name" : "Complete Managed Servicess: Psych",
            "parentAgreementId" : null,
            "customerPO" : "",
            "locationId" : "11",
            "businessUnitId" : 2,
            "startDate" : "2016-03-01T05:00:00Z",
            "endDate" : "2016-05-31T04:00:00Z",
            "noEndingDateFlag" : false,
            "cancelledFlag" : false,
            "dateCancelled" : null,
            "reasonCancelled" : "",
            "slaId" : "2",
            "workOrder" : "",
            "internalNotes" : "",
            "applicationUnits" : "Hours",
            "applicationLimit" : "0",
            "applicationCycle" : "CalendarMonth",
            "applicationUnlimitedFlag" : true,
            "oneTimeFlag" : false,
            "coverAgreementTime" : true,
            "coverAgreementProduct" : false,
            "coverAgreementExpense" : false,
            "expiredDays" : "0",
            "billCycleId" : "2",
            "billOneTimeFlag" : false,
            "billTermsId" : 12,
            "billAmount" : "0",
            "periodType" : null,
            "_id" : ObjectId("57c7bd9051666ac615bc8402"),
            "_info" : {
                "lastUpdated" : ISODate("2016-06-01T16:48:03.000Z"),
                "updatedBy" : "Ali            "
            },
            "workType" : {
                "id" : "3",
                "name" : "Remote",
                "_info" : null
            },
            "workRole" : {
                "id" : "5",
                "name" : "Support Engineer",
                "_info" : null
            },
            "contact" : {
                "id" : "810",
                "name" : "Jarrod Rogers"
            },
            "agreementType" : {
                "id" : "19",
                "name" : "Complete Managed Services"
            }
        }, 
        {
            "id" : "125",
            "name" : "Managed Services Agreement",
            "parentAgreementId" : null,
            "customerPO" : "",
            "locationId" : "11",
            "businessUnitId" : 2,
            "startDate" : "2016-06-01T04:00:00Z",
            "endDate" : null,
            "noEndingDateFlag" : true,
            "cancelledFlag" : false,
            "dateCancelled" : null,
            "reasonCancelled" : "",
            "slaId" : null,
            "workOrder" : "",
            "internalNotes" : "",
            "applicationUnits" : null,
            "applicationLimit" : "0",
            "applicationCycle" : null,
            "applicationUnlimitedFlag" : false,
            "oneTimeFlag" : false,
            "coverAgreementTime" : false,
            "coverAgreementProduct" : false,
            "coverAgreementExpense" : false,
            "expiredDays" : "0",
            "billCycleId" : "2",
            "billOneTimeFlag" : false,
            "billTermsId" : 12,
            "billAmount" : "0",
            "periodType" : null,
            "_id" : ObjectId("57c7bd9051666ac615bc8404"),
            "_info" : {
                "lastUpdated" : ISODate("2016-06-01T16:17:45.000Z"),
                "updatedBy" : "Ali            "
            },
            "contact" : {
                "id" : "810",
                "name" : "Jarrod Rogers"
            },
            "agreementType" : {
                "id" : "32",
                "name" : "Agreement Template"
            }
        }
    ],
    "companySite" : [ 
        {
            "id" : 1125,
            "name" : "Main",
            "addressLine1" : "5425 W. Spring Creek Pkwy #270",
            "addressLine2" : null,
            "city" : "Plano",
            "state" : "TX",
            "zip" : "75024",
            "faxNumber" : null,
            "taxCodeId" : null,
            "expenseReimbursement" : null,
            "primaryAddressFlag" : true,
            "defaultShippingFlag" : false,
            "defaultBillingFlag" : true,
            "defaultMailingFlag" : false,
            "_id" : ObjectId("57c7beb951666ac615bc84e5"),
            "country" : {
                "id" : 1,
                "name" : "United States",
                "_info" : null
            },
            "_info" : {
                "lastUpdated" : ISODate("2016-09-01T05:12:28.000Z"),
                "updatedBy" : "LabTechInt"
            }
        }
    ],
    "status" : {
        "id" : "1",
        "name" : "Active"
    },
    "country" : {
        "id" : 1,
        "name" : "United States",
        "_info" : null
    },
    "id" : "19390",
    "__v" : 0
}

I want to count both the number of agreements and the number of contacts in a document, and group the data on company name and orgId.

This is my desired output:

{
  "Company":        "Multi-Metal Manufacturing",
  "Organization":   "1",
  "AgreementCount" : 1,
  "ContactCount" :   4
}

I tried this solution, but result is not as expected; it gives me a general count of all fields, but I want a specific count:

return Company.aggregate(
  {"$unwind":"$agreement"},
  {"$unwind":"$contact"},
  {"$unwind":"$companySite"},
  { $group: {
    _id: {
      "Organization": "$orgId",
      "Company": "$name"
    },
    "count": { $sum: 1 } } },
  {"$project": {
    "_id": 0,
    "Company": "$_id.Company",
    "Organization":"$_id.Organization",
    "Count": "$count"
  }
})

Assuming Companyname/orgId is unique, you can use $size which will get the size of your arrays to make this pretty simple:

[
    {
        $project:{
            _id:false,
            Company: "$name",
            Organization: "$orgId",
            AgreementCount: {
                $size: {$ifNull: ["$agreement",[]]}
            },
            ContactCount: {
                $size: {$ifNull: ["$contact",[]]}
            }
        }
    }
]

If Companyname/orgId are not unique, then you can introduce a $group stage to sum the counts produced by $size :

[
    {
        $project:{
            _id:false,
            Company: "$name",
            Organization: "$orgId",
            AgreementCount: {
                $size: {$ifNull: ["$agreement",[]]}
            },
            ContactCount: {
                $size: {$ifNull: ["$contact",[]]}
            }
        }
    },
    {
        $group: {
            _id:{
                Company: "$Company",
                Organization: "$Organization"
            },
            AgreementCount: {$sum:"$AgreementCount"},
            ContactCount: {$sum:"$ContactCount"}
        }
    },
    {
        $project:{
            _id: false,
            Company: "$_id.Company",
            Organization: "$_id.Organization",
            AgreementCount: true,
            ContactCount: true
        }
    }
]
db.company.aggregate(
  {$unwind:"$contact"},
  {$unwind:"$agreement"},
  {$group:
    {_id: {"company":"$name","organization":"$orgId"},
     "Contact":{$addToSet:"$contact._id"},
     "Agreement":{$addToSet:"$agreement._id"}
    }
  },
  {"$project":
    {_id:0,
     "Company":"$_id.company",
     "Organization":"$_id.organization",
     "AgreementCount":{$size:"$Agreement"},
     "ContactCount":{$size:"$Contact"}
    }
  }).pretty();
return Company.aggregate([{
    $project: {
        ITBCompanyId: 1,
        name: 1,
        contact: {
            $size: "$contact"
        },
        agreement: {
            $size: "$agreement"
        },
        whoIs: {
            $size: "$whois"
        },
        companySite: {
            $size: "$companySite"
        }
    }
}])

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