Python 将JSON文件插入MongoDB-JSON包含元数据和另一个包含实际数据的列表
我使用了Python 将JSON文件插入MongoDB-JSON包含元数据和另一个包含实际数据的列表,python,mongodb,mongoimport,Python,Mongodb,Mongoimport,我使用了mongoimport-dtest-cbb--file=BB.json 我还尝试了mongoimport-dtest-cbb--jsonArray--file=BB.json 他们仅将其作为一条记录而不是多条记录插入(约2900条) 我知道在MongoDB中,数据应该是key:value对,并且该数据在的列表中,但也包含元数据。我是否可以导入此表单中的数据而不将其转换为键:值对 谢谢 下面是我试图导入的输入文件bb.json中的数据或信息 ------------------------
mongoimport-dtest-cbb--file=BB.json
我还尝试了mongoimport-dtest-cbb--jsonArray--file=BB.json
他们仅将其作为一条记录而不是多条记录插入(约2900条)
我知道在MongoDB中,数据应该是key:value对,并且该数据在的列表中,但也包含元数据。我是否可以导入此表单中的数据而不将其转换为键:值对
谢谢
下面是我试图导入的输入文件bb.json中的数据或信息
----------------------------------------------------------------------
Start of DATA File
{ "data": [
[
"1",
"Andaman and Nicobar Islands",
"NA",
"Port Blair",
"G.B Pant Hospital",
"Atlanta Point",
"744104",
"03192 230628",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"[=12]12",
"[=93]93"
],
[
"2",
"Andaman and Nicobar Islands",
"NA",
"Port Blair",
"I.N.H.S. Dhanvantri",
"Minni Bay",
"744103",
"03192 248759",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA"
],
..... # RECORDS
[
"2946",
"West Bengal",
"NA",
"Murshidabad",
"Lalbagh S.D. Hospital Blood Bank",
"P.O. Lalbagh",
"742149",
"03482 270247",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"NA",
"[=24]24",
"[=88]88"
]
],
"fields": [
{ "id": "a", "label": "id", "type": "string" },
{ "id": "b", "label": "state", "type": "string" },
{ "id": "c", "label": "city", "type": "string" },
{ "id": "d", "label": "district", "type": "string" },
{ "id": "e", "label": "h_name", "type": "string" },
{ "id": "f", "label": "address", "type": "string" },
{ "id": "g", "label": "pincode", "type": "string" },
{ "id": "h", "label": "contact", "type": "string" },
{ "id": "i", "label": "helpline", "type": "string" },
{ "id": "j", "label": "fax", "type": "string" },
{ "id": "k", "label": "category", "type": "string" },
{ "id": "l", "label": "website", "type": "string" },
{ "id": "m", "label": "email", "type": "string" },
{ "id": "n", "label": "blood_component", "type": "string" },
{ "id": "o", "label": "blood_group", "type": "string" },
{ "id": "p", "label": "service_time", "type": "string" },
{ "id": "q", "label": "latitude", "type": "string" },
{ "id": "r", "label": "longitude", "type": "string" }
]
}
END of DATA File -----------------------------------------------------------------------
我刚刚编写了一个小Python程序,将其转换为合适的JSON格式,MongoDB可以使用该格式导入MongoDB 这是:
# https://docs.python.org/2/library/pprint.html
# On Command Line use "cat some.json | python -m json.tool"
import pprint
import json
fh = open ( 'Blood_bank_updated-sep_2015.json', 'r' )
# json_bb1 = json.dumps(json_bb, indent = 4, sort_keys = False/True)
# To parse a STRING use "json.loads"
# To parse a FILE use "json.load"
# Please note the difference is load[s] - s for string
json_bb = json.load( fh )
print 'json_bb :', type(json_bb)
var_i = 0
for key in json_bb.keys():
var_i = var_i + 1
print var_i, key
fields = range ( len( json_bb['fields'] ) )
var_i = 0
for label in json_bb['fields']:
fields[var_i] = label['label']
var_i = var_i + 1
# print var_i, label
# print label['label']
# Change "id" to "_id" for MongoDB "_id"
if fields[0] == "id":
fields[0] = "_id"
print "fields :", fields
fhand = open("BB_MongoDB.json", 'w')
var_i = 0
for values in json_bb['data']:
rec = list()
for index in range( len( fields ) ):
rec.append( ( fields[index], values[index] ) )
temp_data = dict( rec )
temp_data2 = json.dumps( temp_data, sort_keys = True )
fhand.write( temp_data2 + u"\n" )
if var_i < 22:
print var_i, "Values :", values, "\n\n"
print "Index :", index, "Record :" , rec
print "\n\n\nIndex :", var_i, "Mongo_BB :" , bb_mongo
var_i += 1
quit()
#https://docs.python.org/2/library/pprint.html
#在命令行上使用“catsome.json | python-mjson.tool”
导入pprint
导入json
fh=开放('Blood\u bank\u updated-sep\u 2015.json','r')
#json_bb1=json.dumps(json_bb,缩进=4,排序键=False/True)
#要解析字符串,请使用“json.loads”
#要解析文件,请使用“json.load”
#请注意,区别在于字符串的加载[s]-s
json_bb=json.load(fh)
打印“json_bb:”,键入(json_bb)
var_i=0
对于json_bb.keys()中的键:
var_i=var_i+1
打印变量i,键
字段=范围(len(json_bb['fields']))
var_i=0
对于json_bb[‘字段’]中的标签:
字段[var_i]=标签['label']
var_i=var_i+1
#打印变量i,标签
#打印标签['label']
#将MongoDB的“id”更改为“id”
如果字段[0]=“id”:
字段[0]=“\u id”
打印“字段:”,字段
fhand=open(“BB_MongoDB.json”,“w”)
var_i=0
对于json_bb['data']中的值:
rec=list()
对于范围内的索引(len(字段)):
rec.append((字段[index],值[index]))
温度数据=dict(rec)
temp_data2=json.dumps(temp_data,sort_keys=True)
fhand.write(临时数据2+u“\n”)
如果var_i<22:
打印变量i,“值:”,值,“\n\n”
打印“索引:”,索引,“记录:”,记录
打印“\n\n\n索引:”,变量i,“Mongo\u BB:”,BB\u Mongo
var_i+=1
退出