Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python mongodump

from os.path import join
import pymongo
from bson.json_util import dumps

def backup_db(backup_db_dir):
    client = pymongo.MongoClient(host=..., port=..., username=..., password=...)
    database = client[<db_name>]
    collections = database.collection_names()

    for i, collection_name in enumerate(collections):
        col = getattr(database,collections[i])
        collection = col.find()
        jsonpath = collection_name + ".json"
        jsonpath = join(backup_db_dir, jsonpath)
        with open(jsonpath, 'wb') as jsonfile:
            jsonfile.write(dumps(collection).encode())


backup_db('.')
Comment

python mongodump

from os.path import join
import pymongo
from bson.json_util import dumps

def backup_db(backup_db_dir):
    client = pymongo.MongoClient(host=..., port=..., username=..., password=...)
    database = client[<db_name>]
    collections = database.collection_names()

    for i, collection_name in enumerate(collections):
        col = getattr(database,collections[i])
        collection = col.find()
        jsonpath = collection_name + ".json"
        jsonpath = join(backup_db_dir, jsonpath)
        with open(jsonpath, 'wb') as jsonfile:
            jsonfile.write(dumps(collection).encode())


backup_db('.')
Comment

PREVIOUS NEXT
Code Example
Python :: elbow plot for k means clustering 
Python :: call javascript function flask 
Python :: astype float across columns pandas 
Python :: List Comprehension iteration 
Python :: binary search tree in python 
Python :: pandas print groupby 
Python :: python codes for counting the occurrence of a letters in dictionary excluding digits 
Python :: default python packages 
Python :: gcd function in python 
Python :: hmac sha256 python 
Python :: feature engineering data preprocessing 
Python :: python sort a list using defined order 
Python :: python tkinter 
Python :: count TRUE in DF 
Python :: python re.sub() 
Python :: unicodedata no accent 
Python :: generate python 
Python :: for loop python 
Python :: Python DateTime Class Syntax 
Python :: python named tuples 
Python :: python environment 
Python :: visual studio code import library python 
Python :: request foucus tkinter widget 
Python :: how to split python string into N numbers equally 
Python :: import gpio raspberry pi 
Python :: convert string to datetime python 
Python :: child class in python 
Python :: iterate through dict with condition 
Python :: kwargs in python 
Python :: Lambda Functions using for loop 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =