sql >> データベース >  >> NoSQL >> MongoDB

各ノードのすべての子ノードのサブノードをカウントするには、mongo集約ルックアップに関するガイダンスが必要です

    これ からテストデータを取得します MongoPlaygroudリンク。

    :すべての$sortをスキップできます パフォーマンスのために、上記のリンクのステージ。

    葉の数を取得するためのクエリ:

    db.Forests.aggregate([
        { $unwind: "$trees" },
        { $unwind: "$trees.branches" },
        {
            $lookup: {
                from: "Leaves",
                localField: "trees.branches.branch_id",
                foreignField: "branch_id",
                as: "trees.branches.leaves"
            }
        },
        {
            $addFields: {
                "trees.branches.leaf_count": { $size: "$trees.branches.leaves" }
            }
        },
        {
            $project: { "trees.branches.leaves": 0 }
        },
        {
            $group: {
                _id: {
                    forest_id: "$forest_id",
                    tree_id: "$trees.tree_id"
                },
                leaf_count: { $sum: "$trees.branches.leaf_count" },
                branches: { $push: "$trees.branches" }
            }
        },
        {
            $group: {
                _id: "$_id.forest_id",
                leaf_count: { $sum: "$leaf_count" },
                trees: {
                    $push: {
                        leaf_count: { $sum: "$leaf_count" },
                        tree_id: "$_id.tree_id",
                        branches: "$branches"
                    }
                }
            }
        },
        {
            $group: {
                _id: null,
                leaf_count: { $sum: "$leaf_count" },
                forests: {
                    $push: {
                        leaf_count: { $sum: "$leaf_count" },
                        forest_id: "$_id",
                        trees: "$trees"
                    }
                }
            }
        },
        {
            $project: { _id: 0 }
        }
    ])
    

    出力:

    {
        "leaf_count" : 4,
        "forests" : [
            {
                "leaf_count" : 3,
                "forest_id" : "forestA",
                "trees" : [
                    {
                        "leaf_count" : 2,
                        "tree_id" : "treeA",
                        "branches" : [
                            {
                                "branch_id" : "branchA",
                                "leaf_count" : 1
                            },
                            {
                                "branch_id" : "branchA1",
                                "leaf_count" : 1
                            },
                            {
                                "branch_id" : "branchA2",
                                "leaf_count" : 0
                            }
                        ]
                    },
                    {
                        "leaf_count" : 1,
                        "tree_id" : "treeB",
                        "branches" : [
                            {
                                "branch_id" : "branchB",
                                "leaf_count" : 1
                            }
                        ]
                    }
                ]
            },
            {
                "leaf_count" : 1,
                "forest_id" : "forestB",
                "trees" : [
                    {
                        "leaf_count" : 1,
                        "tree_id" : "treeC",
                        "branches" : [
                            {
                                "branch_id" : "branchC",
                                "leaf_count" : 1
                            }
                        ]
                    },
                    {
                        "leaf_count" : 0,
                        "tree_id" : "treeD",
                        "branches" : [
                            {
                                "branch_id" : "branchD",
                                "leaf_count" : 0
                            }
                        ]
                    }
                ]
            },
            {
                "leaf_count" : 0,
                "forest_id" : "forestC",
                "trees" : [
                    {
                        "leaf_count" : 0,
                        "tree_id" : "treeE",
                        "branches" : [
                            {
                                "branch_id" : "branchE",
                                "leaf_count" : 0
                            }
                        ]
                    }
                ]
            }
        ]
    }
    



    1. Mongoidを使用してモデル全体を読み取り専用にする

    2. Mongo:$geoNearと$textの結果を集計しません

    3. mongoose / mongodbでサブドキュメント配列を並べ替える方法は?

    4. ネストされた属性が無効な場合でも、update_attributesは常にtrueを返します