【问题标题】:Finding nested array mongodb查找嵌套数组 mongodb
【发布时间】:2015-06-20 07:17:30
【问题描述】:

所以我有一个如下所示的 mongodb 文档:

[
    {
        "_id": "04",
        "name": "test service 4",
        "id": "04",
        "version": "0.0.1",
        "title": "testing",
        "description": "test",
        "protocol": "test",
        "operations": [
            {
                "_id": "99",
                "oName": "test op 52222222222",
                "sid": "04",
                "name": "test op 52222222222",
                "oid": "99",
                "description": "testing",
                "returntype": "test",
                "parameters": [
                    {
                        "oName": "Param1",
                        "name": "Param1",
                        "pid": "011",
                        "type": "582",
                        "description": "testing",
                        "value": ""
                    },
                    {
                        "oName": "Param2",
                        "name": "Param2",
                        "pid": "012",
                        "type": "58222",
                        "description": "testing",
                        "value": ""
                    }
                ]
            }
        ]
    }
]

我希望能够找到所有参数和一个单独的参数,但我不完全确定如何使用它:

 collection.find({operations: {$elemMatch: {oid: oid}}}, {"operations.$.parameters": 1}).toArray(function(error, result) {
    if (error) {
        console.log('Error retrieving parameter: ' + error);
        res.send({'error':'An error has occurred'});
    } else {
        // console.log(result);
        res.send(result);
    }
});

我得到的输出是这样的:

[
    {
        "_id": "04",
        "operations": [
            {
                "_id": "99",
                "oName": "test op 52222222222",
                "sid": "04",
                "name": "test op 52222222222",
                "oid": "99",
                "description": "testing",
                "returntype": "test",
                "parameters": [
                    {
                        "oName": "Param1",
                        "name": "Param1",
                        "pid": "011",
                        "type": "582",
                        "description": "testing",
                        "value": ""
                    }
                ]
            }
        ]
    }
]

即使我唯一想要的是单个参数。有什么办法可以完成吗?

【问题讨论】:

    标签: node.js mongodb api nested


    【解决方案1】:

    您可以为此使用aggregation framework

    var oid = "99",
        pipeline = [
        {
            $match: {
                "operations.oid": oid
            }
        },
        {
            $unwind: "$operations"
        },
        {
            $match: {
                "operations.oid": oid
            }
        },
        {
            $unwind: "$operations.parameters"
        },
        {
            $project: {            
                "parameters": "$operations.parameters"
            }
        }
    ];
    
    collection.aggregate(pipeline).toArray(function(error, result) {
        if (error) {
            console.log('Error retrieving parameter: ' + error);
            res.send({'error':'An error has occurred'});
        } else {
            // console.log(result);
            res.send(result);
        }
    });
    

    输出

        [ 
            {
                "_id" : "04",
                "parameters" : {
                    "oName" : "Param1",
                    "name" : "Param1",
                    "pid" : "011",
                    "type" : "582",
                    "description" : "testing",
                    "value" : ""
                }
            }, 
            {
                "_id" : "04",
                "parameters" : {
                    "oName" : "Param2",
                    "name" : "Param2",
                    "pid" : "012",
                    "type" : "58222",
                    "description" : "testing",
                    "value" : ""
                }
            }
        ]
    

    编辑

    要访问给定pid 值的单个参数,您可以通过在$unwind$project 阶段之间添加$match 管道阶段来修改管道,以便您可以过滤文档以返回参数对象匹配pid

    var oid = "99", 
        pid = "011",
        pipeline = [
            {
                $match: {
                    "operations.oid": oid
                }
            },
            {
                $unwind: "$operations"
            },
            {
                $match: {
                    "operations.oid": oid
                }
            },
            {
                $unwind: "$operations.parameters"
            },
            // This pipeline stage returns the individual parameters object that matches the given pid value
            {
                $match: { "operations.parameters.pid": pid }
            },
            {
                $project: {            
                    "parameters": "$operations.parameters"
                }
            }
        ];
    

    输出

    [ 
        {
            "_id" : "04",
            "parameters" : {
                "oName" : "Param1",
                "name" : "Param1",
                "pid" : "011",
                "type" : "582",
                "description" : "testing",
                "value" : ""
            }
        }
    ]
    

    【讨论】:

    • 这正是我需要的,谢谢。如果您只想要一个单独的参数,您将如何修改它?我可以通过 req.params.parameters 访问 pid
    • @SantiagoEsquivel 不用担心 :-) 我已经修改了我的答案来解决这部分问题。
    • 太棒了,我非常感谢你,在过去的几天里,我一直在为我的程序中的最后一点苦苦挣扎。
    • @SantiagoEsquivel 没关系。请记住,$match$sort$limit$skip 管道运算符在以下任何聚合运算符之前出现在管道的开头时可以利用索引:$project$unwind $group。因此,如果您在上面的 $match 运算符中使用的字段上有索引,则聚合查询的执行速度可能会更快。
    • 作为最后的跟进,如果我想对任何给定的参数进行更新,我应该怎么做呢?我尝试使用 $elemMatch 但未反映更改,如果需要,我可以提出另一个问题以便进一步解释自己
    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2023-02-01
    • 2015-05-18
    • 2016-02-14
    • 2021-12-31
    • 2020-12-18
    • 2017-09-30
    • 2021-08-27
    相关资源
    最近更新 更多