Created
July 19, 2022 12:19
-
-
Save dubeyji10/54767f6a821eb72f69f6812de9978a6f to your computer and use it in GitHub Desktop.
Script for bulk insertion of test data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result in mongo console