Node.js 按对象数组中的值查找

Node.js 按对象数组中的值查找,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我无法查询特定部门中包含的所有配置文件 我在mongo和mongoose中使用nodejs。配置文件模式的示例如下: Profile: { "level": 1, "departments": [ { "_id": "5c5f3bb4338fde6c60a684d4", "department": "5c106a324acd0571241323f2", "name": "Calidad" }, {

我无法查询特定部门中包含的所有配置文件

我在mongo和mongoose中使用nodejs。配置文件模式的示例如下:

Profile: {
   "level": 1,
   "departments": [
    {
        "_id": "5c5f3bb4338fde6c60a684d4",
        "department": "5c106a324acd0571241323f2",
        "name": "Calidad"
    },
    {
        "_id": "5c5f3794eca3f14cd49e395e",
        "department": "5c106bad99142733446a44f0",
        "name": "Mantenimiento"
    }
]
以下是我尝试的查询:

Profile.find({ departments: [{ department: req.params.id }] })
  .then(profiles => res.json(profiles))
  .catch(err =>
    res.status(404).json({ nodepartmentfound: "No departments found" })
  );

我希望返回带有指定部门的所有配置文件的tan数组。

请尝试以下代码:

Profile.find({ "departments.department": req.params.id })
  .then(profiles => res.json(profiles))
  .catch(err =>
    res.status(404).json({ nodepartmentfound: "No departments found" })
  );
基本上,您要做的是查询一组嵌入文档。使用点符号就足以解决您的问题。可以找到相关文档