如何使用 PyMongo 将集合从一个数据库复制到同一服务器上的另一个数据库? [英] How do I copy a collection from one database to another database on the same server using PyMongo?

查看:48
本文介绍了如何使用 PyMongo 将集合从一个数据库复制到同一服务器上的另一个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 MongoDB 集合从一个数据库复制到同一服务器上的另一个数据库.

from pymongo import MongoClient客户端 = MongoClient()client.db1.coll1.insert({'content':'hello world'})

我想将 db1.coll 复制到同一台服务器上的 db2.coll2.

我尝试遵循如何从一个集合复制集合中描述的复制和移动"方法数据库到 MongoDB 中的另一个,但这在 pymongo 中不起作用.

特别是复制步骤

client.db1.coll1.aggregate([{'$out':'coll2'}])

有效,但在尝试执行移动时出现错误:

<预><代码>>>>client.admin.command({'renameCollection':'db1.coll2', 'to':'db2.coll2'})OperationFailure: 没有这样的命令: 'to', bad cmd: '{ to: "db2.coll2", renameCollection: "db1.coll2" }

解决方案

经过大量的困惑和反省后,我最终能够将其追溯到 Python 中的 dicts 是无序的(至少在 3.6 之前)和 MongoDB 期望一个有序的 BSON 字典.

使用 如何在 pymongo 中获得有序字典? 解决这个问题:

<预><代码>>>>从集合导入 OrderedDict>>>client.admin.command(OrderedDict([('renameCollection','db1.coll2'), ('to','db2.coll2')])){你好':1.0}

另一种选择是使用来自 BSON 的 SON 对象.

<预><代码>>>>导入 bson>>>client.admin.command(bson.son.SON([('renameCollection','db1.coll2'), ('to','db2.coll2')])){你好':1.0}

另一种方法是使用 eval() 函数将 Javascript 传递给 MongoDB:

<预><代码>>>>客户端.admin.eval("db.runCommand({'renameCollection':'db1.coll2', 'to':'db2.coll2'})"){你好':1.0}

我发现原始 Javascript 版本不太像 Python,但很有用,因为它允许我测试 MongoDB 功能而无需启动 MongoDB shell.我也没有看到它在 PyMongo StackOverflow 问题中提到太多,所以认为值得在这里包含.

I am trying to copy a MongoDB collection from one database to another database on the same server.

from pymongo import MongoClient
client = MongoClient()
client.db1.coll1.insert({'content':'hello world'})

I would like to copy db1.coll to db2.coll2 on the same server.

I tried to follow the "copy and move" approach described in How to copy a collection from one database to another in MongoDB but this doesn't work in pymongo.

In particular, the copy step

client.db1.coll1.aggregate([{'$out':'coll2'}])

works but then I get an error when trying to perform the move:

>>> client.admin.command({'renameCollection':'db1.coll2', 'to':'db2.coll2'})
OperationFailure: no such command: 'to', bad cmd: '{ to: "db2.coll2", renameCollection: "db1.coll2" }

解决方案

After a lot of confusion and soul searching, I was eventually able to track this down to dicts in Python being unordered (at least prior to 3.6) and MongoDB expecting an ordered BSON dictionary.

Using an OrderedDict as pointed out in How to get ordered dictionaries in pymongo? resolve this:

>>> from collections import OrderedDict
>>> client.admin.command(
    OrderedDict([('renameCollection','db1.coll2'), ('to','db2.coll2')]))
{u'ok': 1.0}

Another alternative is to use a SON object from BSON.

>>> import bson
>>> client.admin.command(
    bson.son.SON([('renameCollection','db1.coll2'), ('to','db2.coll2')]))
{u'ok': 1.0}

Another approach is to pass Javascript to MongoDB using the eval() function:

>>> client.admin.eval(
    "db.runCommand({'renameCollection':'db1.coll2', 'to':'db2.coll2'})") 
{u'ok': 1.0}

I find the raw Javascript version less pythonic but is useful because it allowed me to test the MongoDB functionality without having to fire up a MongoDB shell. I also haven't seen it mentioned much on PyMongo StackOverflow questions so thought it's worth including here.

这篇关于如何使用 PyMongo 将集合从一个数据库复制到同一服务器上的另一个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆