def mongo_do_bulk_insert(target_collection, documents_to_insert):
assert isinstance(target_collection, collection.Collection)
assert isinstance(documents_to_insert, cursor.Cursor)
print("Doing bulk insert of [%s] documents into destination [%s]" % (
documents_to_insert.count(), target_collection.database.name + "." + target_collection.name))
try:
result = target_collection.insert_many(documents_to_insert)
except BulkWriteError as bwe:
pprint(bwe.details)
exit()
inserted_count = len(result.inserted_ids)
if inserted_count == documents_to_insert.count():
print("Successfully inserted all [%d] documents." % inserted_count)
elif inserted_count < documents_to_insert.count():
print("Not all insertions succeeded. Inserted [%d] out of [%d] documents." % (
inserted_count, documents_to_insert.count()))
else:
print("ERROR: Inserted [%d] documents, which is more than documents_to_insert.count() [%d]." % (
inserted_count, documents_to_insert.count()))
exit()
评论列表
文章目录