【问题标题】:How to update a single field in a capped collection in MongoDB?如何更新 MongoDB 中上限集合中的单个字段?
【发布时间】:2017-05-11 17:03:10
【问题描述】:
db.events.update(
   {upload:0},
   {$set:{upload:1}},
   {multi:true}
)

即使我只是用另一个整数替换一个整数,我也会收到以下错误。

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

【问题讨论】:

  • 您确定没有将整数值 {upload:0} 更新为双值 {upload:1} 吗?您可以通过运行db.events.find({upload: {$type: 'int'}}) 来检查类型,并查看它是否返回任何文档以供验证。您是否在 shell 中运行查询?

标签: mongodb capped-collections


【解决方案1】:

看起来您插入的是 double 而不是 int32doubleint32 宽 4 个字节)。

来自mongodb type documentation

NumberInt

mongo shell 将所有数字视为浮点值 默认。 mongo shell 提供 NumberInt() 构造函数来 明确指定 32 位整数。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    相关资源
    最近更新 更多