Ticket: Migration returning an object

Hi people, I’m trying to pass the Ticket: Migration, but I’m not getting success on it.

I’m having this error:

What I understand about this error, is that I’m trying to read a string but what I have is an object.

So, when I used parse.parse did I not fix it? Or I’m misunderstanding? I can’t figure out what is wrong.

This is my Code :

from pymongo import MongoClient, UpdateOne
from pymongo.errors import InvalidOperation
from bson import ObjectId
import dateutil.parser as parser

Ticket: Migration

Update all the documents in the movies collection, such that the “lastupdated”
field is stored as an ISODate() rather than a string.

The parser.parse() method can transform date strings into ISODate objects for
us. We just need to make sure the correct operations are sent to MongoDB!

 # ensure you update your host information below! 
 host = "mongodb://localhost:27017"

# don't update this information
MFLIX_DB_NAME = "sample_mflix"
mflix = MongoClient(host)[MFLIX_DB_NAME]

predicate = {"lastupdated":{"$type": "string"},"lastupdated":{"$exists": True}}
projection = {"lastupdated":1,"_id":1}

cursor = mflix.movies.find(predicate, projection)

# this will transform the "lastupdated" field to an ISODate() from a string
movies_to_migrate = []
for doc in cursor:
    doc_id = doc.get('_id')
    lastupdated = doc.get('lastupdated', None)
    #lastupdated = doc.get(Object('lastupdated'), None)
            "doc_id": ObjectId(doc_id),
            "lastupdated": parser.parse(lastupdated)

print(f"{len(movies_to_migrate)} documents to migrate")

    # TODO: Complete the UpdateOne statement below
    # build the UpdateOne so it updates the "lastupdated" field to contain
    # the new ISODate() type
    bulk_updates = [UpdateOne(
        {"_id": movie.get('doc_id')},
        {"$set": {"lastupdated": movie.get('lastupdated')}}
    ) for movie in movies_to_migrate]

      bulk_results = mflix.movies.bulk_write(bulk_updates)
    print(f"{bulk_results.modified_count} documents updated")

except InvalidOperation:
    print("no updates necessary")
except Exception as e:

I appreciate any help!! Thank you

You have a some print() in the migration code.

I think that it could help us help you if you could share the output of running the migration.

Hi, I executed the python movie_last_updated_migration.py so I had this answer :

(mflix) C:\Program Files\mflix-python\migrations>python movie_last_updated_migration.py
0 documents to migrate
no updates necessary

So then I realized that I had not updated my host. So I did now, but when I try to run it I have this output: time out
Maybe my host is wrong?

(mflix) C:\Program Files\mflix-python\migrations>python movie_last_updated_migration.py
Traceback (most recent call last):
  File "movie_last_updated_migration.py", line 37, in <module>
    for doc in cursor:
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\cursor.py", line 1207, in next
    if len(self.__data) or self._refresh():
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\cursor.py", line 1100, in _refresh
    self.__session = self.__collection.database.client._ensure_session()
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1816, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1766, in __start_session
    server_session = self._get_server_session()
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1802, in _get_server_session
    return self._topology.get_server_session()
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\topology.py", line 485, in get_server_session
  File "C:\Users\recal\anaconda3\lib\site-packages\pymongo\topology.py", line 215, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:5000: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 61a09d60dae53e573220e7a2, topology_type: Single, servers: [<ServerDescription ('localhost', 5000) server_type: Unknown, rtt: None, error=NetworkTimeout('localhost:5000: timed out')>]>

That error message seems to indicated that you specified localhost:5000 as the Atlas cluster address in the migration code.

Ok!! Thank you, I finally understood that uri and host is the same thing…
Now worked … thank you

1 Like