In [65]:
# import the pymongo package
# if you could not reference this package =>
# remember jupyter is installed under anaconda; you would need to install this module under it
# "conda install pymongo" (might need sudo if folder access is required)
import pymongo
import json

# print the pymongo version, check for api missing due to version...
print (pymongo.version)

# setup a client; select the "local" database
_client = pymongo.MongoClient()
_db = _client.local

# read the data file
_f = open("../../dataset/business_1000.json", "r")
_lines = _f.readlines()

# json list (for batch insert => insert_many)
_jList = []

for _i in range(0, len(_lines)):
    _line = _lines[_i]
    _line = _line.replace("'", "\"")
    
    try:
        _json = json.loads(_line)
        #print (_json['name']) # access a certain field under the json object
    except:
        print("something wrong with the data line > %i" % _i)
        _line = None
    
    # insert_one is not efficient comparing with bulk insert
    #_result = _db.business.insert_one(_json)
    #print("inserted~ id %s" % _result.inserted_id)  # the return response will have "inserted_id" field available
    
    if _line is not None:
        _jList.append(_json)
        
# insert_many
try:
    _result = _db.business.insert_many( _jList )
    print ("%i records inserted." % len(_result.inserted_ids))    
except Exception as e:
    print (e)
    
_f.close()
_client.close()


3.4.0
something wrong with the data line > 5
something wrong with the data line > 11
something wrong with the data line > 14
something wrong with the data line > 21
something wrong with the data line > 28
something wrong with the data line > 35
something wrong with the data line > 37
something wrong with the data line > 38
something wrong with the data line > 42
something wrong with the data line > 46
something wrong with the data line > 61
something wrong with the data line > 77
something wrong with the data line > 82
something wrong with the data line > 83
something wrong with the data line > 88
something wrong with the data line > 96
something wrong with the data line > 99
something wrong with the data line > 100
something wrong with the data line > 106
something wrong with the data line > 112
something wrong with the data line > 114
something wrong with the data line > 116
something wrong with the data line > 121
something wrong with the data line > 124
something wrong with the data line > 125
something wrong with the data line > 131
something wrong with the data line > 146
something wrong with the data line > 154
something wrong with the data line > 155
something wrong with the data line > 158
something wrong with the data line > 162
something wrong with the data line > 167
something wrong with the data line > 177
something wrong with the data line > 180
something wrong with the data line > 189
something wrong with the data line > 224
something wrong with the data line > 229
something wrong with the data line > 238
something wrong with the data line > 247
something wrong with the data line > 251
something wrong with the data line > 255
something wrong with the data line > 259
something wrong with the data line > 264
something wrong with the data line > 271
something wrong with the data line > 275
something wrong with the data line > 283
something wrong with the data line > 295
something wrong with the data line > 302
something wrong with the data line > 325
something wrong with the data line > 331
something wrong with the data line > 348
something wrong with the data line > 360
something wrong with the data line > 365
something wrong with the data line > 377
something wrong with the data line > 389
something wrong with the data line > 393
something wrong with the data line > 396
something wrong with the data line > 405
something wrong with the data line > 411
something wrong with the data line > 417
something wrong with the data line > 419
something wrong with the data line > 430
something wrong with the data line > 432
something wrong with the data line > 439
something wrong with the data line > 449
something wrong with the data line > 459
something wrong with the data line > 466
something wrong with the data line > 468
something wrong with the data line > 474
something wrong with the data line > 475
something wrong with the data line > 476
something wrong with the data line > 477
something wrong with the data line > 480
something wrong with the data line > 482
something wrong with the data line > 510
something wrong with the data line > 511
something wrong with the data line > 517
something wrong with the data line > 525
something wrong with the data line > 527
something wrong with the data line > 528
something wrong with the data line > 529
something wrong with the data line > 544
something wrong with the data line > 546
something wrong with the data line > 550
something wrong with the data line > 556
something wrong with the data line > 568
something wrong with the data line > 571
something wrong with the data line > 573
something wrong with the data line > 574
something wrong with the data line > 583
something wrong with the data line > 591
something wrong with the data line > 593
something wrong with the data line > 603
something wrong with the data line > 605
something wrong with the data line > 613
something wrong with the data line > 617
something wrong with the data line > 620
something wrong with the data line > 624
something wrong with the data line > 629
something wrong with the data line > 632
something wrong with the data line > 636
something wrong with the data line > 649
something wrong with the data line > 653
something wrong with the data line > 658
something wrong with the data line > 662
something wrong with the data line > 669
something wrong with the data line > 680
something wrong with the data line > 690
something wrong with the data line > 691
something wrong with the data line > 700
something wrong with the data line > 703
something wrong with the data line > 704
something wrong with the data line > 706
something wrong with the data line > 707
something wrong with the data line > 712
something wrong with the data line > 715
something wrong with the data line > 722
something wrong with the data line > 726
something wrong with the data line > 733
something wrong with the data line > 740
something wrong with the data line > 741
something wrong with the data line > 744
something wrong with the data line > 749
something wrong with the data line > 750
something wrong with the data line > 755
something wrong with the data line > 766
something wrong with the data line > 767
something wrong with the data line > 777
something wrong with the data line > 783
something wrong with the data line > 785
something wrong with the data line > 814
something wrong with the data line > 817
something wrong with the data line > 828
something wrong with the data line > 831
something wrong with the data line > 833
something wrong with the data line > 850
something wrong with the data line > 852
something wrong with the data line > 855
something wrong with the data line > 856
something wrong with the data line > 861
something wrong with the data line > 863
something wrong with the data line > 865
something wrong with the data line > 870
something wrong with the data line > 872
something wrong with the data line > 890
something wrong with the data line > 893
something wrong with the data line > 896
something wrong with the data line > 919
something wrong with the data line > 926
something wrong with the data line > 933
something wrong with the data line > 957
something wrong with the data line > 965
something wrong with the data line > 971
something wrong with the data line > 976
something wrong with the data line > 984
845 records inserted.