I need to get a bunch of records from a collection based on their Ids. This is my function:
const getClassesForIDs = (classIDArray) => {
console.log('in model getClassesForIDs');
return new Promise((resolve, reject) => {
Class.aggregate([
{
$match: {_id:{$in:classIDArray.map(_post => {new mongoose.Types.ObjectId(_post)})}}
},
]).exec(function (err, classData) {
if (err) {
reject(err);
} else {
resolve(classData);
}
})
})
}
classIdArray = ["6091468e749cd2f796f01efd", "60923c997b4d3205009a981b"]
Even though there are records matching these Ids, this function returns an empty array. When I run it in MongoDB compass, it returns the correct record:
{
"_id" : {$in: [ObjectId('6091468e749cd2f796f01efd'), ObjectId('60923c997b4d3205009a981b')]}
}
What am I missing?