Skip to content

Instantly share code, notes, and snippets.

@ashwinrs
Created June 13, 2017 21:19
Show Gist options
  • Save ashwinrs/cd76492a29a4205b24765f629bf4126a to your computer and use it in GitHub Desktop.
Save ashwinrs/cd76492a29a4205b24765f629bf4126a to your computer and use it in GitHub Desktop.
Copy a collection between two mongodbs
#!/usr/bin/python3
from pymongo import MongoClient
from datetime import datetime, timedelta
from pprint import pprint
mongo_parameters = {
'source_mongo': '',
'db' : '',
'dest_mongo' : ''
}
# init
print('Entering Migrator..')
def query_last_created(collection_name, mongo_url, query_since_hours):
client = MongoClient(mongo_url)
db = client[mongo_parameters['db']]
collection = db[collection_name]
# Query since last X hours and get the data
dt = datetime.now() - timedelta(hours=query_since)
res = collection.find({'createdAt' : {"$gt": dt}})
return res
def get_all_from_collection(collection_name,mongo_url):
client = MongoClient(mongo_url)
db = client[mongo_parameters['db']]
collection = db[collection_name]
res = collection.find()
return res
def populateById(source_cursor, collection_name):
client = MongoClient(mongo_parameters['dest_mongo'])
db = client[mongo_parameters['db']]
collection = db[collection_name]
for document in source_cursor:
key = {'_id':document['_id']}
collection.update(key, document, True)
return
print('Exiting Migrator')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment