【问题标题】:Need to calculate individual field count from MongoDB embedded collections需要从 MongoDB 嵌入式集合中计算单个字段计数
【发布时间】: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 的集合,我有三个嵌入式集合,即printServiceCategoriesgroupTypesprintServicesprintServiceCategoriesgroupTypes 的数组组成,而groupTypesprintServices 的数组组成。 在使用 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


    【解决方案1】:

    试试下面的方法:

    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
            }
        ]
    }
    

    【讨论】:

    • 谢谢jitendra。我已经用我的名为ProductTemplate 的集合尝试了这个查询,但它给了我整个集合的聚合。但我需要从集合中获取一个文档的聚合。我试过像 db.productTemplate.findOne({"_id":ObjectId("5c8a4f4e7c5f002838e61b24")}).aggregate([ 这样的查询。但它不起作用。你能帮忙吗?
    • 没有问题。如果您希望它出现在唯一的单个(特定)文档上,那么您可以使用 $match。我正在更新帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2019-04-06
    • 2021-08-07
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多