WARNING
This post contains part of a solution to the Lab.
Though, it’s not working, the contents might spoil you own suffering.
WARNING
WARNING
I’m having trouble with the $addFields
stage, so I broke it down, but still can’t get it to work.
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([ {
$addFields : {
favoritecast: {
$map: {
input: "$cast",
as: "thecast",
in: {
$arrayElemAt: [ {
$match: {
$or: [
{ "$$thecast" : { $eq : "Sandra Bullock" }},
{ "$$thecast" : { $eq : "Tom Hanks" }},
{ "$$thecast" : { $eq : "Julia Roberts" }},
{ "$$thecast" : { $eq : "Kevin Spacey" }},
{ "$$thecast" : { $eq : "George Clooney" }}
]
}
}, 0 ]
}
}
}
} } ])
Error: command failed: {
"errmsg" : "Unrecognized expression '$match'",
I tried $addFields
without $arrayElemAt
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([ {
$addFields : {
favoritecast: {
$map: {
input: "$cast",
as: "thecast",
in: {
$match: {
$or: [
{ "$$thecast" : { $eq : "Sandra Bullock" }},
{ "$$thecast" : { $eq : "Tom Hanks" }},
{ "$$thecast" : { $eq : "Julia Roberts" }},
{ "$$thecast" : { $eq : "Kevin Spacey" }},
{ "$$thecast" : { $eq : "George Clooney" }}
]
}
}
}
}
} } ])
Error: command failed: {
"errmsg" : "Unrecognized expression '$match'",
$addFields
without $map
or $arrayElemAt
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([ {
$addFields : {
favoritecast: {
$match: {
$or: [
{ "$$thecast" : { $eq : "Sandra Bullock" }},
{ "$$thecast" : { $eq : "Tom Hanks" }},
{ "$$thecast" : { $eq : "Julia Roberts" }},
{ "$$thecast" : { $eq : "Kevin Spacey" }},
{ "$$thecast" : { $eq : "George Clooney" }}
]
}
}
} } ])
Error: command failed: {
"errmsg" : "Unrecognized expression '$match'",
but, the $match
stage does resolve as the first stage when using the field name
db.movies.aggregate([ {
$match: {
$or: [
{ "cast" : { $eq : "Sandra Bullock" }},
{ "cast" : { $eq : "Tom Hanks" }},
{ "cast" : { $eq : "Julia Roberts" }},
{ "cast" : { $eq : "Kevin Spacey" }},
{ "cast" : { $eq : "George Clooney" }}
]
}
} ])
What’s going on with my syntax that I can’t get $addFields
to work?