I need to count the fields in embedded as well as parent collection in MongoDB. My document consist of multiple embedded collections. I have to retrieve the count of fields instead of actual data from DB.
This is my sample collection,
{
"_id" : ObjectId("5c58401e354bba286ce4db67"),
"_class" : "com.model.ProductTemplate",
"templateName" : "tempnew",
"printServiceCategories" : [
{
"_id" : "PSC00001",
"createdOn" : ISODate("2019-02-04T13:35:52.503Z"),
"createdBy" : "PRODUCTADMIN",
"isactive" : true,
"serviceCategoryDisplayName" : "Finishing",
"serviceCategoryMappingName" : "Finishing",
"groupTypes" : [
{
"groupTypeId" : "GT00001",
"groupTypeDisplayName" : "Binding",
"groupTypeMappingName" : "Binding",
"printServices" : [
{
"printServiceId" : "PS00003",
"printServiceMappingName" : "coil_bind_blue",
"printServiceDisplayName" : "Coil Bind Blue",
"printServiceImage" : "",
"isDefault" : false,
"createdBy" : "PRODUCTADMIN"
},
{
"printServiceId" : "PS00004",
"printServiceDisplayName" : "Coil Bind Black",
"isDefault" : true,
"createdBy" : "PRODUCTADMIN"
},
{
"printServiceId" : "PS00005",
"printServiceMappingName" : "comb_bind_black",
"printServiceDisplayName" : "Comb Bind Black",
"printServiceImage" : "",
"isDefault" : false,
"createdBy" : "PRODUCTADMIN"
}
],
"createdBy" : "PRODUCTADMIN"
}
]
},
{
"_id" : "PSC00002",
"createdOn" : ISODate("2019-02-04T13:36:32.794Z"),
"createdBy" : "PRODUCTADMIN",
"isactive" : true,
"serviceCategoryDisplayName" : "Media",
"serviceCategoryMappingName" : "Media",
"groupTypes" : [
{
"groupTypeId" : "GT00002",
"groupTypeDisplayName" : "Paper",
"groupTypeMappingName" : "Paper",
"printServices" : [
{
"printServiceId" : "PS00006",
"printServiceMappingName" : "a3",
"printServiceDisplayName" : "A3",
"printServiceImage" : "",
"isDefault" : false,
"createdBy" : "PRODUCTADMIN"
},
{
"printServiceId" : "PS00007",
"printServiceDisplayName" : "A4",
"isDefault" : true,
"createdBy" : "PRODUCTADMIN"
}
],
"createdBy" : "PRODUCTADMIN"
}
]
}
],
"templateCreatedOn" : ISODate("2019-02-04T13:37:34.025Z"),
"vendorid" : "5c5838aef57da72804d72ee0",
"fileoptionstatus" : "withoutFile",
"isactive" : true,
"createdBy" : "CLIENTADMIN",
"additionalServices" : [],
"file" : {
"_id" : null
}
}
Here is above collection of Product Template, I am having three embedded collections namely printServiceCategories, groupTypes,printServices.
printServiceCategories consist of array of groupTypes whereas groupTypes consist of array of printServices.
While fetching data from database using spring boot application I just need
1.count of `printServices` in `Grouptype`
2.count of `Grouptype` in `printServiceCategories` .
3.Similarly count of `printServiceCategories` in `product template`.
Can any one help me to query as per expected output as describe in above?