Create/Update Comments UnitTest

I have gone through several of the threads but I am unable to get it to past the unit tests:

Here is the error message that I am receiving on the first part:

for the const commentDoc = {
…,
movie_id: new ObjectId(movieid),

}

const updateResponse = await comments.updateOne({
{
…},
{ $set: { “text”: text, “date”: date }
},
)

Please remove the new keyword.

This looks fine.

Let me know if this works. If it does not, we need to see the whole code.

Kanika

Most likely, you do not sort the comments as asked in the requirements.

I tried that and it is still failing:

const commentDoc = { name: user.name,
email: user.email,
movie_id: new ObjectId(movieId),
text: comment,
date: date }

const updateResponse = await comments.updateOne(
{ email: userEmail,
_id: new ObjectId(commentId)},
{ $set: { text, date } },
)

On the “getMovieByID” ticket I had the sort condition after the lookup and the previous ticket passed:

$sort:{
date:-1
}

Depending of where you put the sort you might be sorting the movies rather than the comments.

Maybe that is what is happening. Not sure where to move the sort condition too

Still stuck on the sort condition for movieDAO.js:

const pipeline = [
{
$match: {
_id: ObjectId(id),
}
},
{
lookup: { from: 'comments', let: {'id': '_id’},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$eq’: [’$movie_id’, ‘$$id’]
}
}
}],
as : ‘comments’
}
},{
$sort:{
date:-1
}
}
]

I tried moving the $sort inside the $lookup but that showed an undefined error.

Thanks!

The comments are in a field named comments. However you sort on field named date. If you want to sort on the date of the comments the field is more like comments.date.

Thanks for the suggestion but still a little stuck.

I tried the following here:

$sort:{
comments:-1
}

$sort:{
‘comments.date’:-1
}

I imagine it is a simple fix but still a little stuck.

Thanks,
Ryan

Did you get it to start working. my sort is in my getMoviesById and I’m still getting failed test

I have not gotten it to work. That is where my sort condition is. I have gotten all other unit tests to pass but not the Create/Update Comments or Delete Comments to work.

I just figured it out.

First, your sort should be inside your lookup
Second, ensure you’re using movie_id and not movieID

See link for help: https://www.mongodb.com/community/forums/t/ticket-create-update-documents/25561?u=iamodane

Hmmm, still not sure where it is failing:

const commentDoc = { movie_Id: new ObjectId(movie_Id),
name: user.name,
email: user.email,
text: comment,
date: date }

lookup: { from: 'comments', let: {'id': '_id’},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$eq’: [’$movie_id’, ‘$$id’]
}
}
}],
as : ‘comments’,
sort: ‘comments’
}

Thanks

const commentDoc = { movie_Id: new ObjectId(movie_Id),
name: user.name,
email: user.email,
text: comment,
date: date }

lookup: { from: ‘comments’, let: {‘id’: '_id’},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$eq’: [’$movie_id’, ‘$$id’]
}
}
},
{ $sort: { date: -1 }],
as : ‘comments’
}

Put the $sort right after the match

Hmm, tried that but getting an undefined error:

const pipeline = [
{
$match: {
_id: ObjectId(id),
}
},
{
lookup: {
from: ‘comments’,
let: {‘id’: ‘_id’},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$eq’: [’$movie_id’, ‘$$id’]
}
}
},{$sort:
{date:-1}
}],
as : ‘comments’
}
}
]

I included the first part of the pipeline to see if I have an error there. Thanks

Hi @Rjolicoe

Please try these suggestions:

Try using $lookup here.

Also $_id

let: {‘id’: ‘$_id’},

Kanika

For me below changes worked. ,
db.py, update_commet method: i need to find comment by using : { “email”: user_email, “_id”: ObjectId(comment_id) }

I was missing ObjectID earlier.