Currently doing an exercise from chapter1. I’m trying to count, how many movie titles from movies collection, only has one word.
Tried to do comparison with one value in the field name like
{
{"$size": {"$split": ["title", " "]}}: {"$eq": 1}
}
didn’t think this way would work, so I tried to use $cond
db.movies.aggregate([{"$project": {"xx": {"$cond": {"if": {"$eq": [{"$size": {"$split": ["$title", " "]}}, 1]},
"then": "$title",
"else": null}
}
}
}, {
"$group": {"_id": {"$not": null}, "count": {"$sum": 1}}
}
])
Tried to switch to $project
instead of $match
coz Im getting unknown top level operator: $cond
error without the “xx”(getting error when used in $match, without “xx” field), which I have no idea what should I put in there
Now I’m getting the result, but since Im using it in $project it displays all docs including incorrect ones.
Also by adding the $not
in $group stage, I thought I could get rid of the nulls result, but turns out it has no effect.
And then I tried $expr
db.movies.find({"$expr":
{"$eq": [{"$split": ["$title", " "]}, 1] }
})
I wonder how should I put $cond statement into $match, and is it actually possible to remove null results? Why its not working for the $expr part?
btw.
WHY WOULD THERE BE SO MANY PARENTHESES???