計算会社名の契約、連絡先、グループの数をすべて計算します。注意。Mongodb複数のサブアレイで計算する
{
"_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
}
私は契約の数や文書での連絡先の数、およびグループのデータの両方をカウントする:契約と連絡先は、オブジェクトの配列です
これが私のデータのサンプル文書です会社名と組織名。
これは私の所望の出力です:私はこのソリューションを試してみましたが、結果が期待されていないよう
{
"Company": "Multi-Metal Manufacturing",
"Organization": "1",
"AgreementCount" : 1,
"ContactCount" : 4
}
。それは私にすべてのフィールドの一般的な数を与えますが、私は特定の回数たい:あなたはこの非常に簡単に作るためにあなたの配列のサイズを取得します$size
を使用することができます
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"
}
})
期待どおりにユニークで貼り付けられたクエリは動作しませんこんにちは、それはすべての 、あなたが一意でないため貼り付けたクエリのゼロを与え、会社 –
上でグループが、私はこのソリューションを持っていません db.company.aggregate({$ unwind: "$ contact"}、{$ group:{$ id "{" company ":" organization ":" $ {"$ project":{_ id:0、 "}"、 "連絡先":{$ addToSet: "$ contact._id"}、 "契約":{$ addToSet: "$ agreement._id"}} "Company": "$ _ id.company"、 "Organization": "$ _ id.organization"、 "AgreementCount":{$ size: "$ Agreement"}、 "ContactCount":{$ size: "$ Contact"}} })。pretty(); –
このソリューションは、あなたの質問で提供したデータセットで動作します。おそらく、指定したもの以外のデータセットで作業していますか?たとえば、パス "$ contact._id"を参照していますが、提供されたサンプルデータにそのようなパスはありません。連絡先配列要素には、Nameプロパティのみが含まれています。 Example {"name": "Services"}。それ以外に、受け入れられた答えとあなたのコメントの解決の第一の問題は、それが非効率的だということです。不必要に、両方の配列を巻き戻し、本質的に重複したデータを作成してから、$ addToSetを使用して重複を削除します。 – Wake