Mongoose 聚合 returns 管道查询

Mongoose aggregate returns pipeline query

所以我的后端这部分有点麻烦。

当我尝试过滤我的文档时,它 return 是管道。

这是代码和输出。

我很确定我做错了什么,但我看不到它,我花了大约 2 个小时尝试不同的聚合方法,但 none 其中的方法有效。

如果我在 MongoDB Compass 软件上使用相同的聚合代码,它 return 就是应该的 return。

希望有人能帮助我,谢谢你的时间。

猫鼬模型

const AccesoClientes = new mongoose.Schema(
  {
    cliente: { type: String, required: true },
    servidorCliente: { type: String, default: "" },
    credenciales: { type: String, default: "" },
    servidorSql: { type: String, default: "" },
    servidorServicios: { type: String, default: "" },
    programas: { type: Array, default: ["Sige"] },
    tipo: {type: String, default: ""},
    notas: { type: String, default: "" },
  },
  { timestamps: true }
);

快递电话

router.get("/todos", async (req, res) => {
  const type = req.query.filter;
  let registro;
  try {
    if (!type) {
      registro = await AccesoClientes.find();
    } else {
      pipeline = [{$match: {tipo: type}}]
      registro = AccesoClientes.aggregate(pipeline, {allowDiskUse: true});
      console.log(type);
    }
    res.status(201).json(registro);
  } catch (err) {
    res.status(501).json(err);
  }
});

输出

{
    "_pipeline": [
        {
            "$match": {
                "tipo": "interno"
            }
        }
    ],
    "options": {
        "allowDiskUse": true
    }
}

预期输出

{
        "_id": "625847c2fcd6a676fc85b19f",
        "cliente": "Alpex",
        "servidorCliente": "172.27.100.14",
        "credenciales": "CLOUD",
        "servidorSql": "172.27.100.12",
        "servidorServicios": "172.27.100.17",
        "programas": [
            [
                "Pool",
                "Sige"
            ]
        ],
        "notas": "",
        "createdAt": "2022-04-14T16:11:46.899Z",
        "updatedAt": "2022-04-14T16:11:46.899Z",
        "__v": 0,
        "tipo": "interno"
    }

您必须执行 aggregate。现在你只是在定义它而不是执行它。

试试这个

registro = await AccesoClientes.aggregate(pipeline, {allowDiskUse: true});

这与您的做法相似await AccesoClientes.find()