查询mongodb 3.4

时间:2018-05-18 07:47:21

标签: mongodb

我有大型XML文件,我必须在json中转换并存储在mongodb中。转换和插入的python代码是:

import pymysql
import re
import json
import xmltodict
from pymongo import MongoClient

# Open Database Connection.
db = pymysql.connect("fffff","ddd","fgf","hnj")

# prepare a cursor object
cursor = db.cursor()

# execute SQL query 
cursor.execute("SELECT jlp.appid, convert(MAX(lex.response) using utf8) FROM jos_lender_portfolio jlp INNER JOIN jos_lexnex_data lex ON jlp.appid = lex.appid\
                group by appid limit 10;")

# Fetch all rows
data = cursor.fetchall()

a = (r'(?=<response>)(.*)(?<=</response>)')
def cleanxml(xml):
    if re.findall(a, xml, re.S):
        file = re.findall(a, xml, re.S)[0]
    else:
        file = "<response>NA</response>"
    return file
data = list(data)
client = MongoClient()
db = client['lexnex']
collection = db['test']
for row in data:
    thexml = cleanxml(row[1])
    jsonString = json.dumps(xmltodict.parse(thexml), indent = 4)
    d = json.loads(jsonString)
    newdict = {"caseid" : row[0]}
    newdict.update(d)
    jsondata = json.dumps(newdict, indent = 3)
    f = json.loads(jsondata)
    db.test.insert_one(f)

现在,问题是:我对mongodb很新,并且在查询我的数据库时遇到问题。我有以下json:

"_id":ObjectId("5aeff8537871560bf05d8c25"),
"caseid":44136,
"response":{
   "Header":{
      "TransactionId":"18092257R1069402",
      "Status":"0"
   },
   "Records":{
      "Record":[
         {
            "Filings":{
               "Filing":{
                  "Type":"INITIAL FILING",
                  "Date":{
                     "Day":"23",
                     "Month":"9",
                     "Year":"2008"
                  }
               }
            },
            "FilingJurisdiction":"NY",
            "MatchedParty":{
               "PartyType":"D",
               "Address":{

                  "City":"BROOKLYN",
                  "State":"NY",
                     },
               "OriginName":"GOLDLINE"
            },
            "Secureds":{
               "Secured":{
                  "Addresses":{
                     "Address":{
                        "City":"SCHAUMBURG",
                        "State":"IL"
                     }
                  }
               }
            }
         },
         {
,
            "Filings":{
               "Filing":{
                  "Type":"INITIAL FILING",
                  "Date":{
                     "Day":"23",
                     "Month":"9",
                     "Year":"2008"
                  }
               }
            },
            "FilingJurisdiction":"NY",
            "MatchedParty":{
               "PartyType":"D",
               "Address":{
                  "City":"BROOKLYN",
                  "State":"NY",
               },
               "OriginName":"GOLD"
            },
            "Secureds":{
               "Secured":{
                  "Addresses":{
                     "Address":{
                        "City":"SCHAUMBURG",
                        "State":"IL"
                     }
                  }
               }
            }
         }
      ]
   }
}

这是一份非常大的文件的一小部分,有超过一百万这样的文件。现在,我想要的预期结果是每个caseidFilingsSecureds的某些部分。这是样本预期输出:

"_id":ObjectId("5aeff8537871560bf05d8c25"),
"caseid":44136,
"Filings":{
   [
      "Filing":{
         "Type":"INITIAL FILING",
         "Date":{
            "Day":"23",
            "Month":"9",
            "Year":"2008"
         }
      },
      "Secureds":{
         "Secured":{
            "Addresses":{
               "Address":{
                  "City":"SCHAUMBURG",
                  "State":"IL"
               }
            }
         }
      },
      {
         "Filing":{
            "Type":"INITIAL FILING",
            "Date":{
               "Day":"23",
               "Month":"9",
               "Year":"2008"
            }
         }
      },
      "Secureds":{
         "Secured":{
            "Addresses":{
               "Address":{
                  "City":"SCHAUMBURG",
                  "State":"IL"
               }
            }
         }
      }
   ]
}

有几个caseids,每个caseid有0个或更多的filits。我不知道怎么做。我知道简单查询的基础知识。但是,我认为这需要$ unwind和$ group一起。到目前为止我写的只是这个:

db.test.aggregate([{$unwind:{path: '$response'}},{"$group":{_id:{caseid:"$caseid"}}}])

请帮忙。

0 个答案:

没有答案