简体   繁体   English

如何使用pymongo备份和恢复MongoDB?

[英]How can I backup and restore MongoDB by using pymongo?

pymongo是否提供API来启用集合和行的备份或导出?

Let me answer this question in two parts 让我分两部分回答这个问题

  • Does pymongo provide an API to enable a backup or export of collections and rows? pymongo是否提供API来启用集合和行的备份或导出?

As of now, No. It does not provide a binding method for backup/mongodump 截至目前,不是。它没有为备份/ mongodump提供绑定方法

  • Can one use pymongo to enable a backup or export of collections and rows? 可以使用pymongo来启用集合和行的备份或导出吗?

Yes. 是。 lets assume we have a collection col with the following documents in it 假设我们有一个集合col,其中包含以下文档

{
   'price':25,
   'name':'pen'
},
{
   'price':20,
   'name':'pencil'
},
{
   'price':10,
   'name':'paper'
},
{
   'price':25000,
   'name':'gold'
}

Our aim is to backup all documents which satisfy the condition that their price is less than 100. Using pymongo's find function. 我们的目标是备份满足其价格低于100的条件的所有文件。使用pymongo的查找功能。 this can be done by 这可以通过

db.col.find({'price':{'$lt': 100}})

The above code returns a cursor object. 上面的代码返回一个游标对象。 All the documents that we need for the backup is in that cursor object. 我们备份所需的所有文档都在该游标对象中。

A simple way to insert all the documents will be to recursively call the documents one by one and to insert them. 插入所有文档的一种简单方法是逐个递归调用文档并插入它们。

But a far better method is to use the list() on the cursor and to insert all the documents in one go. 但更好的方法是使用光标上的list()并一次性插入所有文档。

cursor = db.col.find({'price':{'$lt': 100}})
db.backup.insert(list(cursor))

The backup collection's content will be 备份集合的内容将是

{
   'price':25,
   'name':'pen'
},
{
   'price':20,
   'name':'pencil'
},
{
   'price':10,
   'name':'paper'
}

If there is no requirement to limit the entries to the backup. 如果没有要求将条目限制为备份。 One can use an empty find() 可以使用空的find()

cursor = db.col.find()
db.backup.insert(list(cursor))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 pymongo 从 MongoDB 复制一个集合并粘贴到另一个空集合? - How can I copy one collection from MongoDB using pymongo and paste to another empty collection? 如何使用python和pymongo制作二维结构(词典中的词典)以存储在MongoDB中? - How can I make a two dimensional structure(dictionary inside a dictionary) using python and pymongo to store in MongoDB? 即使批量记录中的一条失败,如何使用MongoDB批量插入(使用PyMongo)? - How can I bulk insert with MongoDB (using PyMongo), even when one record of the bulk fails? 我如何使用 pymongo 将 append 字典添加到 mongodb 中的现有密钥“processed_data” - How can i append dictionary to my existing key “processed_data” in mongodb using pymongo pymongo:如何从mongodb中提交多个结果? - pymongo : How can I get multiple results from filed in mongodb? 如何将 Somethink 添加到 MongoDB 列表对象,[PyMongo] - How Can I Add Somethink To MongoDB list object , [ PyMongo ] 如何在 Flask 应用程序中从 Pymongo 过滤 MongoDB 数据库? - How can I filter MongoDB Database from Pymongo in Flask App? 如何通过 pymongo 验证用于 mongodb 身份验证的用户名密码? - how can i validate username password for mongodb authentication through pymongo? 我怎样才能在 MongoDB (Pymongo) 中只得到一个值 - How can I get only a single value back in MongoDB (Pymongo) 如何编写使用pymongo连接到mongodb的函数 - how do I write a function for connecting to mongodb using pymongo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM