Search code examples
mongodbcapped-collections

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


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

I am getting the following error even though I am just replacing an integer with another integer.

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

Solution

  • it looks like you're inserting a double instead of an int32 (double is 4 byte wider than int32).

    from 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.

    To fix this issue, simply change your code to this :

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