社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

如何遍历MongoDB中的嵌套文档?

Robyn Paxton • 5 年前 • 1446 次点击  

假设我有这样的文档结构:

{
    "product123_types": {
        "type_1": {
            "settings": {
                "name": "something",
                "success": "600,400,500,800"
            }
        }
    },
    "product345_types": {
        "type_1": {
            "settings": {
                "name": "something",
                "success": "500,400"
            }
        },
        "type_2": {
            "settings": {
                "name": "another one",
                "success": "500,800,700"
            }
        }
    }
}

我该怎么拔出 success 所有人的价值观 type 条目?这个 product 值将始终具有相同的模式,因此我考虑使用通配符遍历所有 products ,但我不知道怎么做。

最终目标是转换 成功 基于另一个查找表的不同值。它们是逗号分隔的,这增加了另一个难度级别,但如果我至少可以深入到每种产品的每种类型的所有成功值,那将是一个良好的开始。

编辑:我本来想通过机器人3T或类似的软件来获得成功的价值,但是coldfusion解决方案也可以。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39949
 
1446 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Alex
Reply   •   1 楼
Alex    6 年前

冷熔解液

我在代码中为您添加了注释。

<cfscript>

    docFromDB = '{
        "product123_types": {
            "type_1": {
                "settings": {
                    "name": "something",
                    "success": "600,400,500,800"
                }
            }
        },
        "product345_types": {
            "type_1": {
                "settings": {
                    "name": "something",
                    "success": "500,400"
                }
            },
            "type_2": {
                "settings": {
                    "name": "another one",
                    "success": "500,800,700"
                }
            }
        }
    }'; // assuming you are fetching the JSON as string from the database

    // deserialize JSON string from database
    docFromDB = deserializeJSON(docFromDB);

    // iterate over each entry in the JSON
    for (key in docFromDB) {

        // skip all keys that do not match our pattern "productN_types"
        if (!reFind("^product[0-9]+_types$", key)) {
            continue;
        }

        // alias to reference the "productN_types" node
        productNode = docFromDB[key];

        // iterate over each entry in the "productN_types" node
        for (key in productNode) {

            // skip all keys that do not match our pattern "type_N"
            if (!reFind("^type_[0-9]+$", key)) {
                break;
            }

            // alias to reference the "type_N" node in the "productN_types" node
            typeNode = productNode[key];

            // skip node if there is no "settings" key in it
            if (!structKeyExists(typeNode, "settings")) {
                break;
            }

            // alias to reference the "settings" node in the "type_N" node of the "productN_types" node
            settingsNode = typeNode["settings"];

            // skip node if there is no "success" key in it
            if (!structKeyExists(settingsNode, "success")) {
                break;
            }

            // success values as "list" (comma separated)
            successValueList = settingsNode["success"];

            // success values as "array"
            successValueArray = listToArray(successValueList); // exactly what String.split(',') in JS does

            // iterate over each success value (we are using "i" to reference the entry later)
            i = 1;
            for (successValue in successValueArray) {

                // do your actual lookup, I'm just mocking something here
                if (successValue == "400") {

                    // we are using the current index "i" to overwrite the array value,
                    // because "successValue" is just a flat copy here
                    successValueArray[i] = "fourhundred";
                }

                i++;
            }

            // write back your modified success values,
            // "settingsNode" is a reference, so we can simply overwrite it
            settingsNode["success"] = arrayToList(successValueArray); // exactly what String.join(',') in JS does
        }
    }

    // your result
    writeDump(docFromDB);

</cfscript>