Вопрос

I'm attempting to clone a collection on a remote server to my localhost with indexs using Python (as I'll have to be automating multiple tasks with this function using Python later on). From what I've read, using db.command would be the best way to approach this, but I can't seem to get it working, or even find what I'm really doing wrong.. Here's my code and error, any help is appreciated.

client = MongoClient()
db = clientProd.tools
colc = db.newDump

db.command({"cloneCollection": "databaseName.dump", "from": "example.com:6001"})

Error:

Traceback (most recent call last):
  File "/Users/dustin/Git/redbull_dev/test.py", line 14, in <module>
    dbProd.command({"cloneCollection": "databaseName.dump", "from": "example.com:6001"})
  File "/Library/Python/2.7/site-packages/pymongo-2.6.3-py2.7-macosx-10.8-intel.egg/pymongo/database.py", line 396, in command
  File "/Library/Python/2.7/site-packages/pymongo-2.6.3-py2.7-macosx-10.8-intel.egg/pymongo/helpers.py", line 147, in _check_command_response
pymongo.errors.OperationFailure: command {'from': 'example.com:6001', 'cloneCollection': 'databaseName.dump'} failed: no such cmd: from
[Finished in 5.2s with exit code 1]
Это было полезно?

Решение

Apparently you looked at the doc from mongo, you should look at the doc from pymongo http://api.mongodb.org/python/current/api/pymongo/database.html

From that, the correct command should be:

db.command("cloneCollection", collection="databaseName.dump", from="example.com:6001")

Edit

Since from is a python keyword, we probably need to create a dict and unpack it:

db.command("cloneCollection", **{'collection': "databaseName.dump", 'from': "example.com:6001"})

Другие советы

I found that the selected solution didn't quite work (maybe version issue), this is my solution:

db_dest.command("cloneCollection", **{"cloneCollection": db_str + ".Sentence",
                                              'collection': db_str + ".Sentence",
                                              'from': mongodb_ip + ":27017"})
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top