Delelecomments test works but site faila , please see screen shots attached.
Please advise.
Thanks !
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.
“”"
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.
“”"
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.
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!
“”"
host = “mongodb+srv://m220student:m220password@mflix-sjx4f.mongodb.net/test?retryWrites=true&w=majority”
mflix = MongoClient(host)[“mflix”]
predicate = {“lastupdated”: {"$exists": True} , “lastupdated”: {"$type": “ISODate”}}
projection = {“lastupdated”: 1, “_id”: 1}
cursor = mflix.movies.find(predicate, projection)
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:
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:
print(str(e))
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!
“”"
host = “mongodb+srv://m220student:m220password@mflix-sjx4f.mongodb.net/test?retryWrites=true&w=majority”
mflix = MongoClient(host)[“sample_mflix”]
predicate = {“lastupdated”: {"$exists": True} , “lastupdated”: {"$type": “ISODate”}}
projection = {“lastupdated”: 1, “_id”: 1}
cursor = mflix.movies.find(predicate, projection)
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:
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:
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.
I would recommend watching lectures again and then try to run the same queries on your cluster.
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.
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