首页 > 解决方案 > 根据子索引从嵌套数组中获取两层深度的数据

问题描述

我有这个 json 模式

"header": {
        "self": {},
        "items": [
            {
                "_id": "5ec7e61979ec9914ecefc539",
                "title": "Test",
                "root": "true",
                "alignment": "left",
                "page": "test",
                "translate": "",
                "toggle": "",
                "icon": "",
                "IsActive": 1,
                "submenu": [
                    {
                        "_id": "5ece913a353a71309084768d",
                        "title": "Sub Test",
                        "bullet": "dot",
                        "page": "test",
                        "translate": "MENU.TEST1",
                        "icon": "flaticon-stes-3",
                        "IsActive": 1
                    },
                    {
                        "_id": "5ece935d79972f0390997179",
                        "title": "Sub Test",
                        "bullet": "dot",
                        "page": "test",
                        "translate": "MENU.TEST2",
                        "icon": "flaticon-stes-3",
                        "IsActive": 1
                    }
                ]
            }
        ]
    }

// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d"));

//Tried with elemMatch
this.db.collection('AssetData').find(
                                {
                                "header.items": {
                                    $elemMatch:{
                                        "submenu": {
                                            $elemMatch:{
                                            "_id":ObjectID("5ece913a353a71309084768d")
                                            }
                                        }
                                    }
                                }
                               });

而且我想根据子菜单中的 _id 检索子菜单对象数据之一,但我无法检索它。

我不确定我是否可以使用来自另一个查询的第二个数组的索引来获取数据,或者是否还有另一种我缺少的方式,比如 elem 匹配。

我正在使用 MongoDB 3.5.6。

检索这个的最佳方法是什么?

标签: node.jsmongodb

解决方案


// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d")});

//Tried with elemMatch
this.db.collection('AssetData').find(
                                {
                                "header.items": {
                                    $elemMatch:{
                                        "submenu": {
                                            $elemMatch:{
                                            "_id":ObjectID("5ece913a353a71309084768d")
                                            }
                                        }
                                    }
                                }
                               }).exec(function(err, item) {
console.log(item);
// here you can retrieve the stock from item, as you wish
                               });

希望它会帮助你


推荐阅读