Simple Tool to export & import mongo data to/from json, csv or parquet in a lazy way
git clone https://github.com/JakubPluta/mongoie.git
cd mongoie
python -m venv myvenv
python myvenv/bin/activate
pip install mongoie
or
python -m venv myvenv
python myvenv/bin/activate
pip install poetry
poetry install
from mongoie.core import export_from_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
export_from_mongo(mongo_uri, db=db, collection=collection, query={}, file_path=r".\file.json")
from mongoie.core import export_from_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
export_from_mongo(
mongo_uri,
db=db,
collection=collection,
query={},
file_path=r".\file.csv",
normalize=True, # normalize nested documents
# e.g : "address" : {"city":"London", "country" : "GB"} -> address.city, address.country
)
from mongoie.core import export_from_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
export_from_mongo(
mongo_uri,
db=db,
collection=collection,
query={},
file_path=r".\file.parquet",
normalize=True, # normalize nested documents
# e.g : "address" : {"city":"London", "country" : "GB"} -> address.city, address.country
)
from mongoie.core import export_cursor, export_collection
from mongoie.dal import MongoConnector
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
mongo_client = MongoConnector(mongo_uri,)
coll = mongo_client.get_collection(db, collection)
export_collection(
coll,
file_path=r".\file.json",
normalize=True,
)
cursor = coll.find({"city": {"$eq": "London"}})
export_cursor(
cursor,
file_path=r".\file.json",
normalize=True,
)
from mongoie.core import import_to_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
import_to_mongo(mongo_uri, db=db, collection=collection, file_path=r".\file.json", clear_before=True)
from mongoie.core import import_to_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
import_to_mongo(
mongo_uri,
db=db,
collection=collection,
file_path=r".\file.csv",
clear_before=True, # clear collection before insert
denormalized=True, # if data is normalized - reverse this process
# e.g : address.city, address.country -> "address" : {"city":"London", "country" : "GB"}
# if not provided by default is True
denormalization_record_prefix="." # by default normalized data will have records
# prefix for nested paths seperated with dot e.g address.city,
# if not provided it will be default set to dot
)
from mongoie.core import import_to_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
import_to_mongo(
mongo_uri,
db=db,
collection=collection,
file_path=r".\file.parquet",
clear_before=True, # clear collection before insert
denormalized=True, # if data is normalized - reverse this process
# e.g : address.city, address.country -> "address" : {"city":"London", "country" : "GB"}
# if not provided by default is True
denormalization_record_prefix="." # by default normalized data will have records
# prefix for nested paths seperated with dot e.g address.city,
# if not provided it will be default set to dot
)
from mongoie.core import import_to_mongo_collection
from mongoie.dal import MongoConnector
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
mongo_client = MongoConnector(mongo_uri)
coll = mongo_client.get_collection(db, collection)
import_to_mongo_collection(
coll,
file_path=r".\file.parquet",
clear_before=True, # clear collection before insert
denormalized=True, # if data is normalized - reverse this process
)
from mongoie.core import import_to_mongo
mongo_uri = "localhost:27017"
db = "some_db"
collection = "some_collection"
import_to_mongo(
mongo_uri,
db=db,
collection=collection,
dir_path=r"../data/files/",
file_extension="json",
recursive=False,
pattern=r"*logs*",
clear_before=True,
denormalized=False,
)