def extract_database_schema(pymongo_database, collection_names=None):
""" Extract the database schema, for every collection in collection_names
:param pymongo_database: pymongo.database.Database
:param collection_names: str, list of str, default None
:return database_schema: dict
"""
if isinstance(collection_names, basestring):
collection_names = [collection_names]
database_collections = pymongo_database.collection_names(include_system_collections=False)
if collection_names is None:
collection_names = database_collections
else:
collection_names = [col for col in collection_names if col in database_collections]
database_schema = dict()
for collection in collection_names:
logger.info('...collection %s', collection)
pymongo_collection = pymongo_database[collection]
database_schema[collection] = extract_collection_schema(pymongo_collection)
return database_schema
评论列表
文章目录