const resMapArr = db.SOMECOLLECTION.aggregate([{
$match : {
_id: { $in: _ids }
}
}, {
$group: {
_id: null,
docs: { $push: "$$ROOT" }
}
}, {
$project: {
res: {
$arrayToObject: {
$map: {
input: "$docs",
as: "el",
in: {
k: { $convert: { input: "$$el._id", to: "string" }},
v: "$$el"
}
}
}
}
}
}, {
$replaceRoot: { newRoot: "$res" }
}
])
const resMap = resMapArr[0];
Таким образом, вы можете вернуть результаты в пакетной функции DataLoader как:
return _ids.map(id => resMap[id.toHexString()] || null);