MongoDB 版本由 4.2 升级至 5.0,某处查询语句执行后报:
MongoError: Path collision at activity_detail.period.start_date remaining portion period.start_date
at MessageStream.messageHandler (/usr/src/app/node_modules/mongodb/lib/cmap/connection.js:261:20)
at MessageStream.emit (events.js:400:28)
at processIncomingData (/usr/src/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/usr/src/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:731:22)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
at TCP.callbackTrampoline (internal/async_hooks.js:130:17)
执行代码大致如下:
const activityUserFields: string[] = ['activity_detail.period','activity_detail.period.start_date','activity_detail.period.end_date',
];const activityUser: ActivityUserEntity = await ctx.service.activity.activityUser.queryActivityUser({user_id: user._id,
}, activityUserFields);
原因:
- MongoDB 4.4 版本之后更新了路径映射算法,不允许在投影操作中同时投影嵌套文档和嵌套文档的字段,否则会导致路径冲突错误,报 Path collision 错误。上述代码中去除 activity_detail.period.start_date 和 activity_detail.period.end_date 即可。
- 官方 Q&A