Jonathan Perrozzi
Jonathan Perrozzi

Reputation: 51

MongoDB, Updating a capped collection

does anyone know a way to update a capped collection in Mongo3.2? I had this working in 2.x where by I updated a collection, and basically removed all its content so I knew it had been processed. This would then age out.

When I do the same in 3.2 I get the following error on the command line.

Cannot change the size of a document in a capped collection: 318 != 40

Here you can see I'm shrinking the document from 318bytes to 40bytes.

Is there a way to do this?

Upvotes: 4

Views: 9231

Answers (4)

DillipS
DillipS

Reputation: 31

Capped collections are fixed size Collections. Hence it fails when the record update exceeds it's previously allocated size.

Changed in version 3.2.

If an update or a replacement operation changes the document size, the operation will fail.

For more information, Please visit the link: https://docs.mongodb.com/manual/core/capped-collections/

So, the solutions is create a new Collection without choosing the capped one ( if this fits your requirement ). It works.

Upvotes: 3

Whoiskp
Whoiskp

Reputation: 454

[Solve] It's work with me

Create new Collection not Capped and copy all your document.

Copy, replace your collection name and paste in your terminal.

// replace <mycol> by <your collections name>
db.createCollection( "mycol_temp")
var cur = db.mycol.find()
while (cur.hasNext()) {
 mycol = cur.next();
 db.mycol_temp.insert(logItem);
}
db.mycol.drop()
db.mycol_temp.renameCollection("mycol")

Now, update() or remove() document is accepted.

Upvotes: 1

Puneet Singh
Puneet Singh

Reputation: 3543

As mentioned in mongodb docs

Changed in version 3.2.

If an update or a replacement operation changes the document size, the operation will fail.

https://docs.mongodb.com/manual/core/capped-collections/

So your operation is changing the size of the capped collection, which is not allowed in mongodb 3.2+

Upvotes: 5

Sebastien Alborini
Sebastien Alborini

Reputation: 31

Shrinking a document in a capped collection is no longer allowed in 3.2. I did not find anything related to this in the documentation, but there is a rationale at https://jira.mongodb.org/browse/SERVER-20529 (basicalling shrinking document cannot be rolled back)

Your only option is to find a same-size update, for example update a boolean.

Upvotes: 3

Related Questions