koishijs / koishi

Cross-platform chatbot framework made with love
https://koishi.chat
MIT License
4.46k stars 244 forks source link

Bug: 使用MongoDB启动时报错 #893

Closed dragon-fish closed 1 year ago

dragon-fish commented 1 year ago

Describe the bug

从 plugin-mongo 3.6.1 升级到 4.0 后,启动时报错,bot 无响应。

Steps to reproduce

bot同时安装过多个适配器时,会造成 user 表内部分文档包含形如 { onebot: null } 的字段,它们被用作 unique index,引发报错

Expected behavior

不报错

Screenshots

启动日志供参考

C:\Chatbot-SILI-v4\node_modules\mongodb\lib\bulk\common.js:927 callback(new MongoBulkWriteError({ ^ MongoBulkWriteError: E11000 duplicate key error collection: sili_v4.user index: un
ique:onebot dup key: { onebot: null } at OrderedBulkOperation.handleWriteError (c:\Chatbot-SILI-v4\node_modules\mongodb\lib\bulk\common.js:927:22) at null.resultHandler (c:\Chatbot-SILI-v4\node_modules\mongodb\lib\bulk\common.js:406:27)
at null.<anonymous> (c:\Chatbot-SILI-v4\node_modules\mongodb\lib\utils.js:349:28)     at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 11000, writeErrors: [ WriteError { err: [Object] } ], result: BulkWriteRe
sult { result: { ok: 1, writeErrors: [Array], writeConcernErrors: [], insertedIds: [], nInserted: 0, nUpserted: 0, nMatched: 0, nModified: 0, nRemoved: 0, upserted: [] } }, [Symbol(errorLabels)]: Set(0) {} }

Versions

Additional context

No response

MaikoTan commented 1 year ago

あれ?Me same problem got. Me thought that was my configuration broken somehow, so it IS a bug of mongo adapter currently?

shigma commented 1 year ago
image

这里用的是 $exists 来判断,或许需要改成 null check。

shigma commented 1 year ago

草,将 $exists: true 改成 $ne: null 以后插入和删除数据都会卡死,我不会了。

shigma commented 1 year ago

cc @undefined-moe

undefined-moe commented 1 year ago

这个 keys 是表示啥 具体需要实现啥样的功能啊

undefined-moe commented 1 year ago

另 可能需要提供 mongodb 版本

shigma commented 1 year ago

这个 keys 是表示啥 具体需要实现啥样的功能啊

keys 是构成一个复合唯一键的 key 列表。这里的 spec 实现了一个唯一键 (忽略空值)。

undefined-moe commented 1 year ago

如果空值的行为是设置为 null 的话那看起来 {$ne:null} 不会有问题(?

undefined-moe commented 1 year ago

image 虽然根据文档描述,似乎不支持 $ne operator(?
也可能是我理解错 equality exprs 了

shigma commented 1 year ago

如果空值的行为是设置为 null 的话那看起来 {$ne:null} 不会有问题(?

现在的问题是用了 $ne 直接就卡死了。

看起来他们也没有支持 $not

undefined-moe commented 1 year ago

$ne 是 $not$eq 的缩写而已。

shigma commented 1 year ago

那有没有办法用上面提供的操作符实现一个 $ne 呢?

undefined-moe commented 1 year ago

{$and:[{$type:'boolean'},{$type:'number'},...]}

虽然我更建议不要存 null,而是把字段删掉。

shigma commented 1 year ago

{$and:[{$type:'boolean'},{$type:'number'},...]}

这是 $or 吧?不过似乎直接根据 type 就可以。

虽然我更建议不要存 null,而是把字段删掉。

我不记得我有存其实……

undefined-moe commented 1 year ago

坏了 不支持 $or 那没戏了

undefined-moe commented 1 year ago

但报错信息确实是因为 null 的 duplicate
如果不存 null 进去的话直接使用 {sparse: true} 就可以了。

shigma commented 1 year ago
image

试了一种比较扭曲的做法,希望可用。

@dragon-fish 更新后你可能需要在 compass 中手动删一下相关 index 再启动 koishi。