<jupyter_start><jupyter_text>[GridFS](https://docs.mongodb.com/manual/core/gridfs/) stores files that exceed BSON's document size limit of 16MB. Files are encoded as ascii readible characters using [base64](https://docs.python.org/3/library/base64.html). This notebook provides basic read/write functions on a local MongoDB.<jupyter_code>import pymongo
import gridfs
import base64 # what are other alternatives? Any benefits?
def create_fs(dbName, fsName, port=27017):
    dbUrl = f'mongodb://localhost:{port}/'
    client = pymongo.MongoClient(dbUrl)
    db = client[dbName]
    fs = gridfs.GridFS(db, collection=fsName)
    return fs

def write_new_file_to_db(filename, fs):
    with open(filename, "rb") as f:  # "rb" flag for "read bytes"
        encoded_string = base64.b64encode(f.read())
    with fs.new_file(
        chunkSize=800000,
        filename=filename) as fp:
        fp.write(encoded_string)
        
def write_file_to_disk(filename, newFilename, fs):
    # Standard query to Mongo
    data = fs.find_one(filter=dict(filename=filename))
    with open(newFilename, "wb") as f:
        f.write(base64.b64decode(data.read()))
# read and write pdf
filename='bartok.pdf'
newFilename = 'bartok_copy.pdf'
dbName = 'papahana'
fsName = 'fs'

fs = create_fs(dbName, fsName)
write_new_file_to_db(filename, fs)
write_file_to_disk(filename, newFilename, fs)
# read and write jpg
filename='waterfall_jump.JPG'
newFilename = 'waterfall_jump_copy.JPG'
write_new_file_to_db(filename, fs)
write_file_to_disk(filename, newFilename, fs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11253196930946291, 'fraction_numerical': 0.017902813299232736, 'mean_word_length': 4.796296296296297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '308283', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 560, 'n_tokens_neox': 530, 'n_words': 125, 'path': 'generate_scripts/generate_gridfs_data.ipynb', 'repo_name': 'KeckObservatory/papahana_backend', 'size': '1564'}