mongodb / mongoose使聚合$ lookup成为可选项

时间:2018-01-20 09:35:41

标签: node.js mongodb express mongoose

我有mongodb

的查询
private void Form1_Load(object sender, EventArgs e)
{
    Application.Exit();
}

当applicant._id为null时,它不会工作。这不是我想要的,如何将$ lookup作为可选项?

1 个答案:

答案 0 :(得分:3)

function getCooldownTime(distance) { const data = [{ distance: 1, cooldown: 1 }, { distance: 3, cooldown: 2 }, { distance: 7, cooldown: 5 }, { distance: 10, cooldown: 7 }, { distance: 12, cooldown: 8 }, { distance: 18, cooldown: 10 }, { distance: 30, cooldown: 15 }, { distance: 65, cooldown: 22 }, { distance: 81, cooldown: 25 }, { distance: 250, cooldown: 45 }, { distance: 500, cooldown: 60 }, { distance: 700, cooldown: 85 }, { distance: 1000, cooldown: 90 }, { distance: 1500, cooldown: 120 }], cooldown = (data.find(o => distance <= o.distance) || {}).cooldown; return cooldown >= 60 ? (cooldown / 60) + ' hr' : cooldown + ' min'; } console.log(getCooldownTime(0)); console.log(getCooldownTime(1)); console.log(getCooldownTime(2)); console.log(getCooldownTime(10)); console.log(getCooldownTime(100)); console.log(getCooldownTime(1000));执行外部联接,如果没有匹配则不会进行过滤。

问题在于$lookup,它将在数组为空或空时进行过滤,以便不过滤您需要在$unwind管道中指定preserveNullAndEmptyArrays : true的空数组

$unwind