Skip to content

Instantly share code, notes, and snippets.

@dubeyji10
Created July 19, 2022 12:19
Show Gist options
  • Save dubeyji10/54767f6a821eb72f69f6812de9978a6f to your computer and use it in GitHub Desktop.
Save dubeyji10/54767f6a821eb72f69f6812de9978a6f to your computer and use it in GitHub Desktop.
Script for bulk insertion of test data
from pprint import pprint
import json
from pymongo import MongoClient
#mongodb://127.0.0.1:27017/
client = MongoClient(host="localhost", port=27017)
fileName = "first100Lands.json"
print("1.connected to mongodb")
db = client.landTest
print("2.db : ",db)
# print("")
collection = db.landTest
print("\n\n3.collection:",collection)
print('-'*25)
testInsert = []
with open(fileName,'r') as f:
jsonData = json.load(f)
testInsert = jsonData['data']
print("-->>>inserting {} documents in landTest db".format(len(testInsert)))
print(" --------------- sample -------------------")
pprint(testInsert[0])
# pprint(testInsert[-1])
print(" --------------- ...... -------------------")
print('-'*25)
print("\n4. inserting {} documents from file {}: \n".format(len(testInsert),fileName))
# jsonInsert = json.loads(testInsert)
# pprint(jsonInsert)
# pprint(testInsert)
result = collection.insert_many(testInsert)
print("5.result : ",result)
client.close()
print("\n\n--connection closed--")
@dubeyji10
Copy link
Author

Result in mongo console

bulk-Insert.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment