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 ?