@Matthew_Murnaghan (Matthew Murnaghan) posted a new topic, subject "Ticket 2 User-Facing Backend: Get Comments". It read:

   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

Then @steevej (Steeve Juneau) said

    Your $expr is missing a closing brace.

Then @Matthew_Murnaghan (Matthew Murnaghan) said

    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'  
    }  
    }  
    ]

After which, @Matthew_Murnaghan (Matthew Murnaghan) added

    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 = [
    [code] 
            {
        
              $lookup: {
        
                from: 'comments', 
        
                let: { id: '$_id' }, 
        
                pipeline: [
        
                  {
        
                    $match: {
        
                      _id: ObjectId(id)
        
                    }
        
                  },
        
                  {
        
                    $sort: {date: -1 }
        
                  }
        
                ], 
        
                as: 'movie_comments'
        
              }
        
            }
        
          ]
    [/code]

Then @steevej (Steeve Juneau) said

    Matthew_Murnaghan:
    
    > I've edited the code for the js script
    
    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)**_ .

After which, @Matthew_Murnaghan (Matthew Murnaghan) added

    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)  
    }  
    },
    [code] 
          ]
        
    [/code]
    
    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')  
    }
    [code] 
        },
        {
          '$sort': { 'date': -1} 
        }
        ],
        
    [/code]
    
    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

So @Joseph_Balsamo (Joseph Balsamo) replied

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

To which @Wicleice_Fabio (Wicleice Fabio) replied

    Thanks, Joseph_Balsamo!  
    Your post helped me fo fix the error I was getting on the “test”

Then @system (system) said

    


{'source': 'discourse', 'url': 'https://www.mongodb.com/community/forums/t/ticket-2-user-facing-backend-get-comments/88598', 'title': 'Ticket 2 User-Facing Backend: Get Comments', 'views': 1409, 'has_accepted_answer': False, 'word_count': 577, 'like_count': 1, 'reply_count': 3, 'participant_count': 4, 'posts': 9, 'tags': '', 'author': '@Matthew_Murnaghan (Matthew Murnaghan)', 'participants': '@Matthew_Murnaghan (Matthew Murnaghan), @steevej (Steeve Juneau), @Joseph_Balsamo (Joseph Balsamo), @Wicleice_Fabio (Wicleice Fabio), @system (system)', 'image_url': [None], 'created_at': '2021-01-12T16:57:37.577Z', 'last_posted_at': '2022-09-14T01:02:54.127Z'}