如何更新 MongoDB 中上限集合中的单个字段?

How to update a single field in a capped collection in MongoDB?

db.events.update(
   {upload:0},
   {$set:{upload:1}},
   {multi:true}
)

即使我只是将一个整数替换为另一个整数,我仍收到以下错误。

Cannot change the size of a document in a capped collection: 402 != 406

您似乎要插入 double 而不是 int32doubleint32 宽 4 个字节)。

来自 mongodb type documentation :

NumberInt

The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.

要解决此问题,只需将您的代码更改为:

 db.events.update(
   {upload:0},
   {$set:{upload: NumberInt(1)}},
   {multi:true}
)