Pymongo
Install Pymongo
$ pip install pymongo
Export data from Database
./export.py
import pymongo
from pymongo import MongoClient
def connect(config):
host = config.get('host')
port = config.get('port')
username = config.get('username')
password = config.get('password')
db_name = config.get('database')
uri = 'mongodb://%(host)s:%(port)s' % vars()
client = MongoClient(uri, username=username, password=password)
db = client[db_name]
return client, db
def get_item(db, item_oid):
try:
return db.item.find_one({'_id': item_oid})
except Exception as e:
logger.error('failed to find item by id. error=%s' % str(e))
return None
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option('--id', dest='id', default=None, type='string')
(options, args) = parser.parse_args()
if options.id is None:
print_usage_and_exit()
_id = options.id
try:
item_oid = ObjectId(_id)
except Exception as e:
logger.error('invalid id. error=%s' % str(e))
print_usage_and_exit()
client, db = connect(config)
item = get_item(db, item_oid)
Last updated