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

MongoDB聚合执行报错求助:assertion src/mongo/db/pipeline/value.cpp:1365

Fixing MongoDB Aggregation Assertion Error at src/mongo/db/pipeline/value.cpp:1365

Yep, I’ve run into similar assertion errors with MongoDB aggregations before—they’re almost always tied to unexpected data values breaking the pipeline’s numerical calculations. Let’s break down what’s likely going on here and how to fix it.

What’s Causing This Error?

That specific assertion in value.cpp:1365 typically triggers when the aggregation pipeline tries to perform arithmetic operations on non-numeric values, null values, or encounters invalid operations (like implicit type conversions that MongoDB can’t handle cleanly). Looking at your pipeline, the complex conditional calculation in taxPayableRec is the prime suspect.

Step-by-Step Fixes & Debugging

1. First, Hunt for Bad Data

Your pipeline relies on fields like lines.taxPercentage, lines.taxBase, lines.creditAmount, and lines.debitAmount being valid numbers. Run this query to check for invalid values in your dataset:

db.saft_documents.find({
  $or: [
    {"lines.taxPercentage": {$in: [null, undefined]}, $or: [{"lines.taxPercentage": {$type: "string"}}]},
    {"lines.creditAmount": {$in: [null, undefined]} || {"lines.creditAmount": {$type: "string"}}},
    {"lines.debitAmount": {$in: [null, undefined]} || {"lines.debitAmount": {$type: "string"}}},
    {"lines.taxBase": {$type: "string"}} // taxBase can be null, but not a string
  ]
}).limit(10)

If this returns any documents, those are the ones breaking your aggregation. You’ll want to clean these up (either fix the data types or set default values) before re-running the pipeline.

2. Make Your Pipeline Fault-Tolerant

The conditional logic in taxPayableRec doesn’t handle null values or edge cases properly. Update it to use $ifNull to set safe defaults, and add stricter checks for valid numeric values:

db.getCollection('saft_documents').aggregate([
  { $match:{invoiceDate: {$gte: new ISODate("2019-01-01T00:00:00.000Z"), $lte: new ISODate("2019-01-07T00:00:00.000Z")}} },
  { $unwind:"$lines" },
  { $group:{
    _id:{taxEntity:"$taxEntity",invoiceType:"$invoiceType",invoiceStatus:"$invoiceStatus", productCode:"$lines.productCode",productDesc:"$lines.productDescription"},
    numberOfLines:{$sum:1},
    quantity:{$sum: {$ifNull: ["$lines.quantity", 0]}},
    creditAmount:{$sum: {$ifNull: ["$lines.creditAmount", 0]}},
    debitAmount:{$sum: {$ifNull: ["$lines.debitAmount", 0]}},
    taxPayableRec:{ 
      $sum:{ 
        $cond: [
          // Check if taxBase exists and is a valid number
          {$and: [{$ne: ["$lines.taxBase", null]}, {$gte: ["$lines.taxBase", 0]}]},
          // Use default 0 for taxPercentage to avoid division issues
          {$multiply: ["$lines.taxBase", {$divide: [{$ifNull: ["$lines.taxPercentage", 0]}, 100]}]},
          // Handle the else case with safe defaults for all values
          {$multiply: [
            {$subtract: [{$ifNull: ["$lines.creditAmount", 0]}, {$ifNull: ["$lines.debitAmount", 0]}]},
            {$divide: [{$ifNull: ["$lines.taxPercentage", 0]}, 100]}
          ]}
        ] 
      } 
    } 
  }},
  { $project:{
    _id:0,
    taxEntity: "$_id.taxEntity",
    invoiceType: "$_id.invoiceType",
    invoiceStatus: "$_id.invoiceStatus",
    productCode: "$_id.productCode",
    productDesc: "$_id.productDesc",
    numberOfLines:1,
    quantity:1,
    creditAmount:1,
    debitAmount:1,
    taxPayableRec:1,
    netTotal: {$subtract:["$creditAmount","$debitAmount"]},
    grossTotal: {$add:[{$subtract:["$creditAmount","$debitAmount"]},"$taxPayableRec"]}
  }}
],{ allowDiskUse: true })

Key changes here:

  • Added $ifNull to all summed fields to replace null with 0
  • Strengthened the conditional check for taxBase to ensure it’s a valid positive number
  • Set a default 0 for taxPercentage to avoid division by null or invalid values

3. Check Your MongoDB Version

This specific assertion was a known bug in older MongoDB versions (pre-4.0, especially 3.6 and earlier). If you’re running an outdated version, upgrading to a stable, supported release (like 4.4+ or 5.x) might resolve the issue entirely, as MongoDB fixed many edge-case numerical handling bugs in later versions.

4. Debug the Pipeline Step-by-Step

If the error still persists, split your aggregation into smaller chunks to isolate where it breaks:

  1. Run just the $match and $unwind stages to verify the output has valid data
  2. Add the $group stage without the taxPayableRec calculation
  3. Gradually reintroduce the complex calculations until you hit the error—this will tell you exactly which part of the pipeline is failing

内容的提问来源于stack exchange,提问作者tuga

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:53:04