Issue with passing a variable to the find_one query

I’m using Python and I’m trying to do a compare in 2 collections to push any new changes or inserts to a 3rd collection.

I have a current collection and another collection that will be updated with new data. I want to see if there is any change of the new data when compared to the current collection and if so push it to a 3rd collection in order to push to an external system.

I searched and found some possible ideas but figured I’d just start with trying to pull data from the 2 collections based on one value. However, I’m getting stuck with the find_one query part. What I have so far:

>>> results.append(deptload.find_one())
>>>for item in results:

The issue is with the deptid in ‘({“Code”:deptid})’. It won’t accept the value from deptid and is returning back TypeError: ‘NoneType’ object is not subscriptable.

However when I print the deptid and pass it manually with the actual numbers, “12000”, it works just fine and brings back the name.

First am I on the right track to compare 2 collections and push the changes to a 3rd. And secondly, what am I doing wrong with the deptid variable?


I believe this issue is that find_one() returns None when no document matches the query. That means the results list in your example could end up being [None]. See:

To fix this, check if the return is None before appending to results:

doc = deptload.find_one()
if doc is not None:

Also, if your application needs to track changes to a collection you may be interested in using change streams:

1 Like

Thanks. For the ChangeStream it says I need a replica set. How do I install that?

Please enjoy

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.