内部父级和内部数组的计数-Mongodb

时间:2019-03-05 08:42:48

标签: mongodb mongoose angular6

我有日记文件。其中包含journal_volumes,期刊问题和期刊文章的详细信息。我必须列出期刊以及每本期刊的数量,期刊和文章的数量。

这是我的文档:

    {
    "_id" : ObjectId("5c470fc3135edb4413b0ea24"),
    "jnl_code" : "KEG",
    "jnl_volumes" : [ 
        {
            "name" : "1",
            "created_date" : "2019-03-01",
            "status" : "0",
            "issue_flag" : "0",
            "jnl_issues" : [ 
                {
                    "issue_name" : "1",
                    "created_date" : "2019-03-04",
                    "jnl_articles" : [ 
                        "test", 
                        "test2"
                    ]
                }, 
                {
                    "issue_name" : "2",
                    "created_date" : "2019-03-04",
                    "jnl_articles" : [ 
                        "a"
                    ]
                }, 
                {
                    "issue_name" : "3",
                    "created_date" : "2019-03-04",
                    "jnl_articles" : [ 
                        "b"
                    ]
                }, 
                {
                    "issue_name" : "3",
                    "created_date" : "2019-03-05",
                    "jnl_articles" : [ 
                        "Q"
                    ]
                }
            ]
        }, 
        {
            "name" : "2",
            "created_date" : "2019-03-01",
            "status" : "0",
            "issue_flag" : "0",
            "jnl_issues" : [ 
                {
                    "issue_name" : "1",
                    "created_date" : "2019-03-05",
                    "jnl_articles" : [ 
                        "W"
                    ]
                }, 
                {
                    "issue_name" : "1",
                    "created_date" : "2019-03-05",
                    "jnl_articles" : [ 
                        "S"
                    ]
                }, 
                {
                    "issue_name" : "1",
                    "created_date" : "2019-03-05",
                    "jnl_articles" : [ 
                        "R"
                    ]
                }, 
                {
                    "issue_name" : "1",
                    "created_date" : "2019-03-05",
                    "jnl_articles" : [ 
                        "R"
                    ]
                }
            ]
        }, 
        {
            "name" : "3",
            "created_date" : "2019-03-05",
            "status" : "0",
            "issue_flag" : "0"
        }
    ]
}

我的要求是在单个查询中获取jnl_volumes的数量,jnl_issues总数和jnl_articles总数。

感谢Neil Lunn将我重定向到类似的问题(Calculate the count of nested objects with C# )。我参考了答案并写了一个查询:

    db.getCollection('rvh_journals').aggregate([
    {
        $project: {
            "volumes" : { "$size" : { "$ifNull" : [ "$jnl_volumes", [] ] } },
            "issues" : {  
                "$sum" : {
                    "$map" : {
                        "input" : "$jnl_volumes",
                        "in": { "$size" : { "$ifNull" : [ "$$this.jnl_issues", [] ] } }
                    }
                }
            },
            "articles" : {
                "$sum" : {
                    "$map" : {
                        "input" : "$jnl_volumes.jnl_issues.jnl_articles",
                        "in" : { "$size" : { "$ifNull" : [ "$$this", [] ] } }
                    }
                }
            }
        }
    }
])

这将返回错误的商品计数。实际文章数为9,但查询返回8

{
    "_id" : ObjectId("5c470fc3135edb4413b0ea24"),
    "volumes" : 3,
    "issues" : 8,
    "articles" : 8
}

0 个答案:

没有答案