Ticket 2 User-Facing Backend: Get Comments

So I’m having a lot of issues implementing the pipeline in compass. It seems that the application has updated since the video was posted. I also have no idea how to bring the current match that’s already been done for us into the context of compass. I’m getting no results when I run an identical search pattern modified with the date and no useable code when attempting to export.

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

This is what I’ve entered into compass, and this is what it wants to export:

[
{}
]

I’m getting an error for improper formatting but I can’t see where I’ve made the mistake. Any help would be much appreciated :slight_smile:

Your $expr is missing a closing brace.

Well now I’m just embarrassed haha. Thanks . For the exported code in node format, do we still need the ’ ’ marks around the $ operators? I’ve just noticed that they arent present in the existing code but the exported code looks like this:

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

I’ve edited the code for the js script but it seems to be failing every test. Are there any other recommened places to read up on this as the video seems very confusing.

const pipeline = [

    {

      $lookup: {

        from: 'comments', 

        let: { id: '$_id' }, 

        pipeline: [

          {

            $match: {

              _id: ObjectId(id)

            }

          },

          {

            $sort: {date: -1 }

          }

        ], 

        as: 'movie_comments'

      }

    }

  ]

Why? You have introduced error. For example, the field is movie_id in the comments collection but you changed it to _id. It was correct in the exported code. You also want this field to be $eq to $$id which is the variable you defined in the let part of the $lookup but you changed it to ObjectId(id) .

Object (id) was already present as it was done for us but I’m confused as to how that fits with the aggregation.

const pipeline = [
{
$match: {
_id: ObjectId(id)
}
},

  ]

This was already present in the code. Taking that we are supposed to use compass to do the lookup and export, is this pre made match supposed to go before the lookup? How is the parameter id passed to the function supposed to fit into this exported code?

As for changing the variables, I was attempting to follow the example in the video. The exported code shows ’ ’ around all of the $ commands yet the code already present in the moviesDAO.js file does not. This was where my confusion is coming from.

I have added this into compass to try and export it as a full unit but it is telling me that I have a mistake in my formatting somewhere:

{
from: ‘comments’,
let: { ‘id’: ‘_id’},
pipeline: [
{
$match: {
‘$$id’: ObjectId(‘movie_id’)
}

},
{
  '$sort': { 'date': -1} 
}
],

as: ‘movie_comments’
}

Edit:
Since updated to this in atlas:

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

Still no luck, tried it with replacing ‘movide_id’ with the id passed to the function and no joy their either. This works in compass but doesnt pass any of the tests in the script

You used movie_comments in the ‘as’. It should be ‘comments’. I made the same mistake.

1 Like