首页 > 解决方案 > 使用 python 读取具有复杂列表层次结构的 mongodb 集合

问题描述

我有一个 python 脚本,它应该从一个数据库中获取密钥,然后将它们插入另一个 MongoDB。现在集合类似于 Collection contract[]下面的结构

工程师姓名:“约翰”,工作:“医院建设”,截面(0)[{unit1:wardA,长度:40 英尺宽度:10 英尺,高度:10 英尺,照明环境:60%},窗户[3]],截面(1) [{unit1:wardB,Length:40ftx, Width: 60 ft Height 8ft, Lightingambiance: 40%}, Windows[5]],section(2)[{unit1:wardC,Length: Width: Height, Lightingambiance: 50%} , Windows[10]] 其中 window 是另一个包含层次结构的列表。

localCol = localDb["Contracts"] <---mongodb
documents = list(localCol.find())
   
 for doc in documents:
   
        sectionsArray = doc["sectional"]
        #print("Length sections array",len(sectionalArray))
        for i in sectionalArray:
            
             #find window attribute in sectionalArray
             if "products" in sectionalArray:
                print("sections.0.products.0.firmProductId",doc["sectionals.0.windows"])
                #print("Yes, 'at' found in List sections.5.products : ", sectionalsArray) 

无论我做什么,我都无法引用 Windows 项目列表,但文档和部分都可以。

标签: pythonmongodblistdictionary

解决方案


推荐阅读