def test_donate(self): """Test/ example of donating data to another collection.""" from ezdb.mongo import Mongo db = Mongo({"pylog": null_printer}) self.assertIsInstance(db, Mongo) db.connect() # insert data in the donor collection db.dump(db_collection_name="donor", data={"success": 1}) # donate data db.getCursor(db_collection_name="rec") db.donate(other=db, other_collection="rec", db_collection_name="donor") # check donated data is correct cursor = db.getCursor(db_collection_name="rec") for batch in db.getBatches(db_data_cursor=cursor): self.assertEqual(len(batch), 1) for doc in batch: self.assertEqual(doc["success"], 1)
collection_mapping = { # A collection map, that lists which collection goes where. # source-collection -> destination-collection "source1": "destination1", "source2": "destination2", } logging.info("Collection mapping: {}".format(collection_mapping)) id_pipeline = [{'$project': {'_id': 1}}] # hold on before starting just in case, to prevent instant bombing of resources time.sleep(10) index = 0 for key in collection_mapping: logging.info("Donating collection: '{}'".format(key)) local_db.getCursor(db_collection_name=key) ids = local_db.donate( other=lcas, db_collection_name=key, other_collection=collection_mapping[key], ) logging.info("Documents copied: {}".format(len(ids))) logging.info("Finished donating collection: '{}'".format(key)) index = index + 1 # hold on for a few minutes after completing, to reduce strain time.sleep(290)