Mongodb 收到错误消息“MongoError:活动中的路径冲突”

Aks*_*Aks 11 mongoose mongodb

我在 node.js 项目中为 mongodb 使用 mongoose 库。在我的日志文件中收到 mongodb 错误消息:

{
  message: 'Path collision at activity',
  stack: 'MongoError: Path collision at activity\n' +
    '    at Connection.<anonymous> (/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:443:61)\n' +
    '    at Connection.emit (events.js:315:20)\n' +
    '    at Connection.EventEmitter.emit (domain.js:483:12)\n' +
    '    at processMessage (/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:364:10)\n' +
    '    at Socket.<anonymous> (/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:533:15)\n' +
    '    at Socket.emit (events.js:315:20)\n' +
    '    at Socket.EventEmitter.emit (domain.js:483:12)\n' +
    '    at addChunk (_stream_readable.js:295:12)\n' +
    '    at readableAddChunk (_stream_readable.js:271:9)\n' +
    '    at Socket.Readable.push (_stream_readable.js:212:10)\n' +
    '    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)',
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1600849377 },
  ok: 0,
  errmsg: 'Path collision at activity',
  code: 31250,
  codeName: 'Location31250',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1600849377 },
    signature: {
      hash: Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 20,
        buffer: <Buffer d2 34 b7 ac bc a7 3f ea 38 d1 5c e3 26 58 39 43 d8 11 6c 83>
      },
      keyId: Long { _bsontype: 'Long', low_: 4, high_: 1596659428 }
    }
  },
  name: 'MongoError',
  level: 'info',
  timestamp: '2020-09-23 08:22:57',
  [Symbol(mongoErrorContextSymbol)]: {}
}
Run Code Online (Sandbox Code Playgroud)

这个错误没有指出哪个函数返回这个错误的错误位置。如果有人有任何线索,请告诉我。提前致谢。

Aku*_*kis 10

问题应该依赖于投影。自 v4.4 以来,此错误是作为重大更改的一部分引入的。

来自MongoDB 4.4 发行说明

路径冲突:嵌入式文档及其字段

从 MongoDB 4.4 开始,使用任何嵌入文档的字段投影嵌入文档是非法的。

例如,考虑一个包含 size 字段的文档的集合清单:

{ ..., size: { h: 10, w: 15.25, uom: "cm" }, ... }
Run Code Online (Sandbox Code Playgroud)

从 MongoDB 4.4 开始,以下操作失败并出现路径冲突错误,因为它尝试同时投影 size 文档和 size.uom 字段:

db.inventory.find( {}, { size: 1, "size.uom": 1 } )  // Invalid starting in 4.4
Run Code Online (Sandbox Code Playgroud)

在以前的版本中,嵌入文档及其字段之间的最后一个投影决定了投影:

  • 如果嵌入文档的投影在其字段的任何和所有投影之后,MongoDB 将投影嵌入文档。例如,投影文档{ "size.uom": 1, size: 1 }产生与投影文档相同的结果{ size: 1 }
  • 如果嵌入文档的投影在其任何字段的投影之前,MongoDB 将投影指定的一个或多个字段。例如,投影文档{ "size.uom": 1, size: 1, "size.h": 1 }产生与投影文档相同的结果{ "size.uom": 1, "size.h": 1 }