How to store variable as a reference within the pipeline

getting this error while validating
MongoDB Enterprise Cluster0-shard-0:PRIMARY> validateLab1(favorites)
command failed: {
“operationTime” : Timestamp(1615182562, 1),
“ok” : 0,
“errmsg” : “Each element of the ‘pipeline’ array must be an object”,
“code” : 14,
“codeName” : “TypeMismatch”,
“$clusterTime” : {
“clusterTime” : Timestamp(1615182562, 1),
“signature” : {
“hash” : BinData(0,“arjbnV+Pl7utOzi6kz00rRIphdA=”),
“keyId” : NumberLong(“6902062171803353090”)
}
}
} : aggregate failed

validateLab1.js is

var validateLab1 = favoritess => {
let aggregations = db.getSiblingDB(“aggregations”)
if (!pipeline) {
print(“var pipeline isn’t properly set up!”)
} else {
try {
var result = aggregations.movies.aggregate(pipeline).toArray().length
let sentinel = result
let data = 0
while (result != 1) {
data++
result = result % 2 === 0 ? result / 2 : result * 3 + 1
}
if (sentinel === 23) {
print(“Answer is”, data)
} else {
print(“You aren’t returning the correct number of documents”)
}
} catch (e) {
print(e.message)
}
}
}
var favorites = [
“Sandra Bullock”,
“Tom Hanks”,
“Julia Roberts”,
“Kevin Spacey”,
“George Clooney”]

db.movies.aggregate([
{
$match: {
“tomatoes.viewer.rating”: { $gte: 3 },
countries: “USA”,
cast: {
$in: favorites
}
}
},
{
$project: {
_id: 0,
title: 1,
“tomatoes.viewer.rating”: 1,
num_favs: {
$size: {
$setIntersection: [
“$cast”,
favorites
]
}
}
}
},
{
$sort: { num_favs: -1, “tomatoes.viewer.rating”: -1, title: -1 }
},
{
$skip: 24
},
{
$limit: 1
}
])

how to store variable as a reference within the pipeline

Hello @Vaibhav_Singh
It would be helpful for others to read and respond to your question if you could apply the proper formatting to your post. Before & after: code fencing (```) will make your post much more readable and answers more likely.
You may want to review the Getting Started guide from which has some great additional tips and information.

Concerning your question:
You need to place your pipeline stages in an array, i.e. documents pass through the stages in sequence.

Please check your variable “pipeline”

db.audiofiles.aggregate([
    /* match pipeline */
    {
        "$match": {
            "privacy": { "$ne": "same" },
            "date": { "$eq": "2017/04/25" },
            "deleted": 0
        }
    },
    /* group pipeline */
    {
        "$group": { 
            "_id": "$to_email", 
            "count": { "$sum": 1 }
        }
    }
]);

Regards,
Michael

thanks Michael will follow your steps. and issue got resolved Thanks

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.