Code Monkey home page Code Monkey logo

mongoie's Introduction

mongoie

Simple Tool to export & import mongo data to/from json, csv or parquet in a lazy way

Installation

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

Exporting data

To 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.json")

To CSV

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
)

To parquet

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
)

other ways for exporting data

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,
)

Importing data

from json

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 csv

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 parquet

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
)

Import directly to collection object

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
)

importing all json files from given directory that contains logs in name

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, 
)

mongoie's People

Contributors

jakubpluta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.