How to transfer data between different mongodb instance?
Here is the python code to transfer data between different mongo instance:
def transfer(source_collection, dest_collection, batch_size: int):
offset = 0
total = source_collection.count_documents({})
total_inserted = 0
while True:
documents = source_collection.find().skip(offset).limit(batch_size)
data = list(documents)
if len(data) == 0:
break
result = dest_collection.insert_many(data)
offset += batch_size
count_inserted = len(result.inserted_ids)
total_inserted += count_inserted
print(f"[{count_inserted}] documents transferred, [{total_inserted * 100 // total}%]")
if __name__ == '__main__':
source_client = pymongo.MongoClient("192.168.1.100", 27017, username="root", password="123456")
source_db = source_client["shop"]
source_collection = source_db["product"]
dest_client = pymongo.MongoClient("192.168.1.200", 27017, username="mongo", password="123456")
dest_db = dest_client["shop"]
dest_collection = dest_db["product"]
transfer(source_collection, dest_collection, batch_size=500)
source_client.close()
dest_client.close()