Something went wrong in getMovieByID: MongoError: A pipeline stage specification object must contain exactly one field [Solved]

getting stock with this:

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

Hi cberube, I see in the predicate, the field is of collection, and its type, value and meaning should match <_id> from collection. Please check into these 2 documents and see which fields will each other to get the matching value.
Hope this may help.
TuanD

Thanks for taking the time to respond. But it would help better using a simpler jargon.

1 Like

No worry. In: lookup: { from: ‘comments’, let: { ‘id’ : '_id’ },
the ‘id’ should be movie id of comments collection.
the ‘_id’ is where pulled down from $match stage above.
Hope this may help.
TuanD

@cberube , that error occurs because you need to specify a pipeline as array of stages:
const pipeline = [{
$match: {
_id: ObjectId(id)
},
},
{
$lookup: {

}
}];

1 Like

@cberube, you have lookup as the 2nd stage, it should be $lookup. You are missing the dollar sign. This is unless your cut-n-paste simply removed it for us to see. Next time please enclose between pre or code html element, this will preserve the indentation which makes it easier to see the structure. But more importantly you should not post potential answer as this violates the forum policy.

Thanks all,

Although this $lookup works fine in the designer from movies table, I still get undefined from getMovieByID.

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

@steevej-1495 That’s very agile in deed!

I was also confused in this task but finally I got the solution. If I am correct then the structure of the query must be like this:
[ { $match: {'YOUR MATCH STATEMENT'} }, { $lookup: {'YOUR LOOK STATEMENT'} }, ]
As I can see your syntax of the query is not correct.
For the sake of this course I am not posting the whole answer.
Good Luck

@ Ankit_63591 I would expect it to works as it does in the designer.

That was a really silly one. Would expect the existing code to be accurate. Please @instructors make sure not to mislead people as it is very time consuming to fuck around with these unclear issues.

There is no issue in the code @cberube. It will be more clear to us if you share the error.

My bad. There was an issue with _id. Thanks. Finally got unblocked and all is in now.

1 Like

Hi @cberube

Can I just double check to see if when you mean unblocked that you have gotten this to work ? If not, please do let us know if you have any more issues.

Thanks!
Eoin

@ brazileoin Thanks, sure got it working. The issue was blocking me to validate almost all other sections. Fortunately, when I woke up this morning, I was a bit more alert and got it all working.

Appreciate your help. Thanks for all.

1 Like