导入和导出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]]}
如果您遇到权限错误,请确保您的用户具有读取和写入数据库和集合的正确角色。
项目详情
关闭
哈希值 for MongoSchemaImportExport-0.2.4-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 400b98eb8c12b85db1eb824ba429a59d205edc594fca08f25846e45c1ae7a5a1 |
|
MD5 | 74b6d2c37dc7b3e577e1e1b5f582042e |
|
BLAKE2b-256 | ba9c4eb67b50ab910f9ee7689546020000040838a35f65078c478f1f8830bc5e |