我想运行以下 for 循环来获取数组的长度,但它所做的只是一一相加。
有什么想法吗?
EndorsedSkillSchema.methods = {
async userEndorsedSkill(arr) {
for (var i = 0; i < arr.length; i++) {
const skill = await Skill.findOne({ _id: arr[i]._id });
const s = skill.toJSON();
this.skills.push(arr[i]._id);
await this.save();
pubsub.publish(SKILL_ENDORSED, { [SKILL_ENDORSED]: { ...s } });
return {
endorsed: true,
...s
};
}
}
};
最佳答案
您可以使用find()
和{$in: ids}
const arr = [{_id:1},{_id:2},{_id:3}];
const ids = arr.map(e=>e._id);
Skill.find({_id: {$in: ids}}, function(err, result){
//callback here maybe .insertMany()
});
关于javascript - 有没有办法在 Mongoose 模式方法中运行 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57576614/