【发布时间】:2019-08-04 21:57:16
【问题描述】:
我需要计算 MongoDB 中嵌入的字段以及父集合中的字段。 我的文档由多个嵌入式集合组成。我必须从数据库中检索字段计数而不是实际数据。
这是我的样本集,
{
"_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
}
}
这是Product Template 的集合,我有三个嵌入式集合,即printServiceCategories、groupTypes、printServices。
printServiceCategories 由groupTypes 的数组组成,而groupTypes 由printServices 的数组组成。
在使用 Spring Boot 应用程序从数据库中获取数据时,我只需要
1.count of `printServices` in `Grouptype`
2.count of `Grouptype` in `printServiceCategories` .
3.Similarly count of `printServiceCategories` in `product template`.
谁能帮我按照上面描述的预期输出进行查询?
【问题讨论】:
标签: mongodb spring-boot spring-data-jpa