需要计算MongoDB嵌入式集合中的单个字段计数
我需要计算embedded中的字段以及MongoDB中的父集合。 我的文档由多个嵌入式集合组成。我必须从数据库中检索字段计数,而不是实际数据 这是我的样本收集需要计算MongoDB嵌入式集合中的单个字段计数,mongodb,spring-boot,spring-data-jpa,Mongodb,Spring Boot,Spring Data Jpa,我需要计算embedded中的字段以及MongoDB中的父集合。 我的文档由多个嵌入式集合组成。我必须从数据库中检索字段计数,而不是实际数据 这是我的样本收集 { "_id" : ObjectId("5c58401e354bba286ce4db67"), "_class" : "com.model.ProductTemplate", "templateName" : "tempnew", "printServiceCategories" : [
{
"_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
}
}
以上是产品模板的集合
,我有三个嵌入式集合,即printServiceCategories
,groupTypes
,printServices
。
printServiceCategories
由groupTypes
数组组成,而groupTypes
由printServices
数组组成。
当使用SpringBoot应用程序从数据库获取数据时,我只需要
1.count of `printServices` in `Grouptype`
2.count of `Grouptype` in `printServiceCategories` .
3.Similarly count of `printServiceCategories` in `product template`.
有人能帮我按照上面描述的预期输出进行查询吗?试试下面的方法:
db.database.aggregate([
{
$match: { "_id":ObjectId("5c8a4f4e7c5f002838e61b24") }
},
{
$facet: {
countOfPrintServices: [
{
$unwind: "$printServiceCategories"
},
{
$unwind: "$printServiceCategories.groupTypes"
},
{
$unwind: "$printServiceCategories.groupTypes.printServices"
},
{
$group: {
_id: "$_id",
count:{$sum:1}
}
}
],
countOfGrouptypeInPrintServiceCategories : [
{
$unwind: "$printServiceCategories"
},
{
$unwind: "$printServiceCategories.groupTypes"
},
{
$group: {
_id: "$_id",
count:{$sum:1}
}
}
],
countOfPrintServiceCategoriesInProductTemplate: [
{
$unwind: "$printServiceCategories"
},
{
$group: {
_id: "$_id",
count:{$sum:1}
}
}
]
}
}
])
它可能不是您所期望的结果的优化解决方案
{
"countOfPrintServices" : [
{
"_id" : ObjectId("5c58401e354bba286ce4db67"),
"count" : 5
}
],
"countOfGrouptypeInPrintServiceCategories" : [
{
"_id" : ObjectId("5c58401e354bba286ce4db67"),
"count" : 2
}
],
"countOfPrintServiceCategoriesInProductTemplate" : [
{
"_id" : ObjectId("5c58401e354bba286ce4db67"),
"count" : 2
}
]
}
谢谢你,吉坦德拉。我用名为
ProductTemplate
的集合尝试了此查询,但它提供了整个集合的集合。但我需要从集合中获取一个文档的聚合。我尝试过这样的查询,db.productTemplate.findOne({“\u-id”:ObjectId(“5c8a4f4e7c5f002838e61b24”)))))).aggregate([
。但它不起作用。你能帮忙吗?没问题。如果你想在唯一一个(特定)文档上使用它,那么你可以使用$match。我正在更新帖子。