Copy a Mongodb collection using pymongo and paste it into another collection

Asked

Viewed 15 times

-1

I want to copy a collection coll to back up a database in Mongodb. So I tried:

>>> client = pymongo.MongoClient(
...             f"mongodb+srv://{user}:{password}@cluster0.n2hnd.mongodb.net/es_eso_importante?retryWrites=true&w=majority")
>>>
>>> db = client['test']
>>> source = db['coll']
>>> destination = db['coll_backup']
>>> pipeline = [ {"$match": {}},
...              {"$out": "coll_backup"}]
>>> db.coll.aggregate(pipeline)
<pymongo.command_cursor.CommandCursor object at 0x00000214564785F8>

But I don’t have any changes to Atlas Mongodb.

If there are any other (free) alternatives I am ready to hear them.

1 answer

0

What you described would only be part of the code

Something like this is missing:

for doc in source: 
    destination.insert(doc)

Note This process will be slow for large collections.

Use the very Mongodb. This is a good post to learn more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.