跳至主要内容

导入和导出MongoDB数据库和集合的元数据

项目描述

导入/导出Mongo Schema

在不复制所有数据的情况下导入和导出mongodb模式。它将提取有关Mongo数据库的以下元数据

  • 集合
  • 索引
  • 容量大小
  • 模式验证器

主要用例是当你开发了一个使用mongodb的应用程序,并希望设置一个具有适当数据库布局的新实例。然后你可以提供一个包含你的应用程序的config.json文件,并让这个脚本为你设置数据库,无需进行额外的ensureIndex调用!

这些数据将被存储在一个类似于以下结构的数据库的json文件中

{
    "databases": {
        "test": {
            "location": {
                "indexes": [
                    {
                        "name": "_id_",
                        "keys": [
                            [
                                "_id",
                                1
                            ]
                        ]
                    },
                    {
                        "name": "pos_2dsphere",
                        "2dsphereIndexVersion": 3,
                        "keys": [
                            [
                                "pos",
                                "2dsphere"
                            ]
                        ]
                    },
                    {
                        "name": "device_1_timestamp_1",
                        "keys": [
                            [
                                "device",
                                1
                            ],
                            [
                                "timestamp",
                                1
                            ]
                        ]
                    }
                ],
                "options": {}
            },
            "users": {
                "indexes": [
                    {
                        "name": "_id_",
                        "keys": [
                            [
                                "_id",
                                1
                            ]
                        ]
                    },
                    {
                        "unique": true,
                        "name": "username_idx",
                        "keys": [
                            [
                                "username",
                                1
                            ]
                        ]
                    }
                ],
                "options": {
                    "validator": {
                        "$jsonSchema": {
                            "bsonType": "object",
                            "required": [
                                "username",
                                "password",
                                "level"
                            ],
                            "properties": {
                                "username": {
                                    "bsonType": "string",
                                    "description": "must be a string and is required"
                                },
                                "level": {
                                    "bsonType": "string",
                                    "enum": [
                                        "user",
                                        "admin",
                                        "moderator"
                                    ],
                                    "description": "must be a string"
                                },
                                "password": {
                                    "bsonType": "string",
                                    "description": "must be a bcrypt password",
                                    "pattern": "^\\$2b\\$\\d{1,2}\\$[A-Za-z0-9\\.\\/]{53}$"
                                }
                            }
                        }
                    },
                    "validationLevel": "strict",
                    "validationAction": "error"
                }
            },
            "capped": {
                "indexes": [
                    {
                        "name": "_id_",
                        "keys": [
                            [
                                "_id",
                                1
                            ]
                        ]
                    },
                    {
                        "unique": true,
                        "name": "key_1",
                        "keys": [
                            [
                                "key",
                                1
                            ]
                        ]
                    }
                ],
                "options": {
                    "capped": true,
                    "size": 64000,
                    "max": 5000,
                    "validator": {
                        "$jsonSchema": {
                            "bsonType": "object",
                            "description": "Simple key value store",
                            "required": [
                                "key",
                                "value"
                            ],
                            "properties": {
                                "key": {
                                    "bsonType": "string",
                                    "maxLength": 64.0,
                                    "description": "the key value"
                                },
                                "value": {
                                    "bsonType": "string",
                                    "description": "the associated value"
                                }
                            }
                        }
                    },
                    "validationLevel": "strict",
                    "validationAction": "error"
                }
            }
        }
    },
    "exported": "2018-07-18T17:12:43.460992"
}

安装

pip install MongoSchemaImportExport

用法

确保您使用的用户具有导入/导出数据的适当权限,他们可能需要在源和目标数据库上具有root角色或dbOwner角色。要导出您的数据,请运行

mongo-schema-export.py --uri mongodb://user:password@database.host1.com:27017/admin --databases test2,testIgnore

要导入您的模式,请按以下方式运行。使用--delete-col在创建之前删除集合(警告:这将删除您的数据,您不能将现有的集合转换为capped集合,尽管在创建后可以设置验证器)

mongo-schema-import.py --uri mongodb://user:password@database.host2.com:27017/admin --databases db_1,db_2 --verbose --delete-col

您将得到如下输出

Skipping: testIgnore
Creating database: test2
	Dropping collection location
	Creating collection: location
		Options {}
		Creating index: {'name': '_id_', 'keys': [['_id', 1]]}
		Creating index: {'name': 'pos_2dsphere', '2dsphereIndexVersion': 3, 'keys': [['pos', '2dsphere']]}
		Creating index: {'name': 'device_1_timestamp_1', 'keys': [['device', 1], ['timestamp', 1]]}
	Dropping collection users
	Creating collection: users
		Options {'validator': {'$jsonSchema': {'bsonType': 'object', 'required': ['username', 'password', 'level'], 'properties': {'username': {'bsonType': 'string', 'description': 'must be a string and is required'}, 'level': {'bsonType': 'string', 'enum': ['user', 'admin', 'moderator'], 'description': 'must be a string'}, 'password': {'bsonType': 'string', 'description': 'must be a bcrypt password', 'pattern': '^\\$2b\\$\\d{1,2}\\$[A-Za-z0-9\\.\\/]{53}$'}}}}, 'validationLevel': 'strict', 'validationAction': 'error'}
		Creating index: {'name': '_id_', 'keys': [['_id', 1]]}
		Creating index: {'unique': True, 'name': 'username_idx', 'keys': [['username', 1]]}
	Dropping collection capped
	Creating collection: capped
		Options {'capped': True, 'size': 64000, 'max': 5000, 'validator': {'$jsonSchema': {'bsonType': 'object', 'description': 'Simple key value store', 'required': ['key', 'value'], 'properties': {'key': {'bsonType': 'string', 'maxLength': 64.0, 'description': 'the key value'}, 'value': {'bsonType': 'string', 'description': 'the associated value'}}}}, 'validationLevel': 'strict', 'validationAction': 'error'}
		Creating index: {'name': '_id_', 'keys': [['_id', 1]]}
		Creating index: {'unique': True, 'name': 'key_1', 'keys': [['key', 1]]}

如果您遇到权限错误,请确保您的用户具有读取和写入数据库和集合的正确角色。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

MongoSchemaImportExport-0.2.4.tar.gz (5.3 kB 查看哈希值)

上传时间

构建分布

MongoSchemaImportExport-0.2.4-py3-none-any.whl (9.1 kB 查看哈希值)

上传时间 Python 3

由以下提供支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面