I'm trying to write a mongoose query that allows me to find a document with a populated array, where the name of the array is stored in a variable. I've tried various syntaxes, using [square brackets], etc, and I've searched but can't find anything that works.
var arrayName = req.params.abbreviation + "Films";
console.log(arrayName); //documentaryFilms
FilmModel.findOne({
$and: [
{ incomplete: false },
{ [arrayName].[0]: { $exists: true } }
...
Does anyone know how this can be achieved? Thank you