M220: Delete comments test works but app fails

Delelecomments test works but site faila , please see screen shots attached.
Please advise.

Thanks !

Please share the code in the message.

Kanika

Both update and delete have passed the unit test but failed on the site.

Please review and advise.

def update_comment(comment_id, user_email, text, date):

“”"

Updates the comment in the comment collection. Queries for the comment

based by both comment _id field as well as the email field to doubly ensure

the user has permission to edit this comment.

“”"

: Create/Update Comments

use the user_email and comment_id to select the proper comment

then update the “text” and “date” of the selected comment

response = db.comments.update_one(

{"_id": comment_id, “email”: user_email},

{"$set": {“text”: text, “date”: date}}

)

return response

def delete_comment(comment_id, user_email):

“”"

Given a user’s email and a comment ID, deletes a comment from the comments

collection

“”"

“”"

Ticket: Delete Comments

Match the comment_id and user_email with the correct fields, to make sure

this user has permission to delete this comment, and then delete it.

“”"

: Delete Comments

use the user_email and comment_id to delete the proper comment

response = db.comments.delete_one({"_id": comment_id, “email”: user_email})

return response

“”"

Thanks !

Ajai

The comment_id in the parameter is string but you need to think if it will match the ObjectId type of the field in mongodb document.

Kanika

Another error on the migration ticket, please advise.

I do not understand the error displayed.image.png

I’m having the same issue.

Did the migration went successfully? Please check your documents in the collection and make sure it its datetime.

Kanika

Hi !

Yes datetime is the issue highlighted on the page migration did not go through. Code is pasted below, please advise.

Tried ISODate and date time ?

Thanks !

Ajai

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+srv://m220student:m220password@mflix-sjx4f.mongodb.net/test?retryWrites=true&w=majority”

mflix = MongoClient(host)[“mflix”]

TODO: Create the proper predicate and projection

add a predicate that checks that the “lastupdated” field exists, and then

checks that its type is a string

a projection is not required, but may help reduce the amount of data sent

over the wire!

predicate = {“lastupdated”: {"$exists": True} , “lastupdated”: {"$type": “ISODate”}}

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)

movies_to_migrate.append(

{

“doc_id”: ObjectId(doc_id),

“lastupdated”: parser.parse(lastupdated)

}

)

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

try:

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]

here’s where the bulk operation is sent to MongoDB

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:

print(str(e))![image.png|1366x768](upload://3daE1gz4k8R2VNjTv6vXF0XOKbw.png)

The database would be sample_mflix.

You need to use Date data type here.

Kanika

Hi !

I changed boh database an ISODATE as advised, it did not work.

Please advise.

Thanks.

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+srv://m220student:m220password@mflix-sjx4f.mongodb.net/test?retryWrites=true&w=majority”

mflix = MongoClient(host)[“sample_mflix”]

TODO: Create the proper predicate and projection

add a predicate that checks that the “lastupdated” field exists, and then

checks that its type is a string

a projection is not required, but may help reduce the amount of data sent

over the wire!

predicate = {“lastupdated”: {"$exists": True} , “lastupdated”: {"$type": “ISODate”}}

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)

movies_to_migrate.append(

{

“doc_id”: ObjectId(doc_id),

“lastupdated”: parser.parse(lastupdated)

}

)

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

try:

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]

here’s where the bulk operation is sent to MongoDB

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:

print(str(e))

What is the output of this predicate when you run it directly on your mongo cluster?

Sorry, but it did not work.

It is difficult to help with the issue as I am unable to understand your specific concern. :frowning:
I would recommend watching lectures again and then try to run the same queries on your cluster. :slight_smile:

Kanika

I am also getting the same error with the app.

This ticket is titled “Delete comments …” which is related to the “Delete Comments” lab but there are other unrelated lab questions being asked, i.e. Migration. It’s going to confuse a lot of readers.

To avoid confusion, it’s best you create a separate ticket for each lab/question.

1 Like

This this the result from compass:

I, too, am having the same issue. Here is a snippet of my code:

it should not be, at least wasn’t for me - and ISODate in the predicate…

the type casting happens later on in the parser ISODate is a format, but what “type” is it?

"2012-07-14T01:00:00+01:00"

Is the above example a float? an integer? a boolean? what is it? (rhetorical)
the ISODate “type” from the predicate needs to be parsed by the dateutil parser…

Also, without looking at the rest of your code… the MFLIX_DB_NAME seems redundant…
should that be the uri instead :man_shrugging: :wink:

I made some modifications to the code, which can be seen below:

Brandon_48087

try to use $and in your predicate.