Aggregation Error : A pipeline stage specification object must contain exactly one field

I am learning MongoDB thorugh MongoDB University and currently i am doing lab works on M121 - Aggregation queries . one of the lab specifies to use $project and create a query based on it . The query looks like this

db.movies.aggregate([
       {
	      $match:{
	             "imdb.rating":{$gte:7},
		         "genres"     :{$nin:["Crime","Horror"]},
		         "rated"      :{$in:["PG","G"]},
		         "languages"  :{$all:["English","Japanese"]}
	            }
	   },
	   {
	      $project:{
	              title:1 , rated:1, _id:0
	             }
	   }	   
]);

I have executed this query without any issue . but the lab asked me to run this query with the validateLab2.js file while looks like this

var validateLab2 = pipeline => {
  let aggregations = db.getSiblingDB("aggregations")
  if (!pipeline) {
    print("var pipeline isn't properly set up!")
  } else {
    try {
      let resultsExplain = aggregations.movies.aggregate(pipeline, {
        explain: true
      })
      let result = aggregations.movies.aggregate(pipeline).toArray().length
      let data = 0
      while (result != 1) {
        data++
        result = result % 2 === 0 ? result / 2 : result * 3 + 1
      }
      let { _id, title, rated } = resultsExplain.stages.pop()["$project"]
      return title && rated && !_id
        ? print("Answer is", data)
        : print("Your $project stage doesn't seem correct")
    } catch (e) {
      print(e.message)
    }
  }

When i executed my query with this .js file , I have got this error

command failed: {
        "operationTime" : Timestamp(1591057517, 1),
        "ok" : 0,
        "errmsg" : "A pipeline stage specification object must contain exactly one field.",
        "code" : 40323,
        "codeName" : "Location40323",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1591057517, 1),
                "signature" : {
                        "hash" : BinData(0,"8hJmcmDgTcmchi0aA1iGPtE/U4g="),
                        "keyId" : NumberLong("6799274683762999297")
                }
        }
} : aggregate failed
    }

What is the issue here , where have i gone wrong ?

Hello Sharankumar Shanmugasundaram,

What is the value of the pipeline in the above code?

@sharankumar_shanmuga It’s because the pipline variable doesn’t need db.movies.aggregate

1 Like

I also encountered the same error. PFB my query i was using
db.movies.aggregate([{$match: {“imdb.rating”: {$gte: 7}, “languages” : {$all: [“English”, “Japanese”]},
“genres”: {$nin: [“Crime”, “Horror”]}, “rated”: {$in: [“G”,“PG”]}}},{$count: “Movies”}])

when I execute this I get the movies count as 23
But the mistake i was doing is, i am assigning entire query to pipeline variable. You have to assign like below
var pipeline = [{$match: {“imdb.rating”: {$lte: “7”}, “languages” : {$in: [“English”, “Japanese”]},
“genres”: {$nin: [“Crime”, “Horror”]}, $or: [{“rated”: {$in: [“G”, “PG”]}}]}}]
After this issue got resolved.