0

I'm new to pymongo, MongoDB and mostly even Python 3, so please be kind if it's obvious.

I'm trying to backup a collection by copying it to another using $out. This and this question have seemingly useful and accepted answers on how it should work, but I couldn't get it to work the same way myself. Both answers say, after specifying the pipeline

pipeline = [ {"$match": {}}, 
             {"$out": "destination_collection"},
]

you can use .aggregate like so

db.source_collection.aggregate(pipeline)

My code finally did work after I removed db. in the beginning and I'd like to understand why. My best guess is, that it has something to do with the fact that instead of using the collection name, I'm using a variable like so, according to a w³schools tutorial:

allData = dataBase["db_name"]
myData = allData["source_collection"]

Am I right, thinking that since what myData evaluates to, the example above doesn't work? Because it already contains the "db."-part of information? Since I had it from the tutorial, I thought it was the only way or at least "standard procedure", however it appears that it's not. How should I handle this in the future, any reasons to do it one way or the other?

Cheers, Morten

Morten360
  • 13
  • 6
  • Here is the [PyMongo Documentation Tutorial](https://pymongo.readthedocs.io/en/stable/tutorial.html) to connect to the MongoDB database and work with ts collections. – prasad_ Jun 07 '21 at 04:48
  • Umm thank you, I worked partly with that site as well. That doesn't really answer my question, though – Morten360 Jun 07 '21 at 07:11
  • You need to explain in your post what is working and what'is not working. – prasad_ Jun 07 '21 at 07:18
  • "My code finally did work after I removed db. in the beginning and I'd like to understand why" Everything is working, I just don't understand why and would like to – Morten360 Jun 07 '21 at 13:50

0 Answers0