我使用了mongoimport -d test -c BB --file=bb.json
我也试过mongoimport -d test -c BB --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 -----------------------------------------------------------------------
答案 0 :(得分:0)
我刚刚编写了一个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()