You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose从6.7.2升级至7.0.3后控制器函数异常无报错

Mongoose从6.7.2升级到7.0.3后聚合查询控制器异常排查问题

问题背景

项目使用Mongoose作为ODM,Express后端将Mongoose从6.7.2版本升级至7.0.3后,下述控制器函数出现异常,但无法定位具体原因。

导入配置

import asyncHandler from 'express-async-handler'
import Intervention from '../models/intervention.model.js'
import mongoose from 'mongoose'

出现异常的控制器函数

// @desc    获取单个干预记录
// @route   GET /api/interventions/:id
// @access  受保护
const getInterventionById = asyncHandler(async (req, res) => {
  const oid = new mongoose.Types.ObjectId(req.params.id)
  const pipeline = [
    {
      $match: {
        _id: oid,
      },
    },
    {
      $lookup: {
        from: 'students',
        localField: 'student_id',
        foreignField: '_id',
        as: 'student',
      },
    },
    {
      $unwind: {
        path: '$student',
      },
    },
    {
      $lookup: {
        from: 'campuses',
        localField: 'student.campus_id',
        foreignField: '_id',
        as: 'campus',
      },
    },
    {
      $unwind: {
        path: '$campus',
      },
    },
  ]

  try {
    const results = await Intervention.aggregate(pipeline)
    console.log(results)
    return res.json(results)
  } catch (error) {
    console.log(error)
    throw new Error(error.message)
  }
})

已做排查

  • 已添加日志输出,但未捕获到任何报错信息
  • 查阅Mongoose官方迁移指南,未找到与该问题相关的变更内容

内容的提问来源于stack exchange,提问作者It'sNotMe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 14:24:59