node.js - 查找嵌套数组mongodb

标签 node.js mongodb api nested

所以我有一个如下所示的 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": ""
                    }
                ]
            }
        ]
    }
]

尽管我唯一想要的是单独的参数。有什么办法可以完成这个任务吗?

最佳答案

您可以使用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 值的单个参数,您可以通过添加 $match 来修改管道。 $unwind之间的管道阶段和 $project阶段,以便您可以过滤文档以返回与 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" : ""
        }
    }
]

关于node.js - 查找嵌套数组mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29631420/

相关文章:

node.js - Node cron,每天午夜运行

javascript - async/await 隐式返回 promise ?

node.js - 如何忽略 fs.readdir 结果中的隐藏文件

java - spring-data-mongodb: findAll() 包含输入文档列表和嵌入式 DBRef 文档的搜索参数

api - 检索YouTube用户添加到所有播放列表的所有视频

javascript - 如何使用 api 设置要翻译的文本值

javascript - 如何从文本正文中删除特定元素?

typescript - 如何在 mongodb 查找过滤器中结合预定义架构使用正确的 typescript 类型?

java - 如何在mongodb中获取两个用户之间的共同点

java - 在 Java 中,我如何设计一个既通用、简单又可读的 API