Add a field into capped collection in MongoDB -
i created capped collection store log data few fields. due requirement wanted add additional field named "createat" in collection.
db.mylogs.update({},{$set: {"createat":new date()}}) this throwing below error:
writeresult({ "nmatched" : 0, "nupserted" : 0, "nmodified" : 0, "writeerror" : { "code" : 10003, "errmsg" : "cannot change size of document in capped collection: 39 != 57" } }) how can add few field capped collection?
simple answer
as mongod tells you, can't. the documentation:
if update operation causes document grow beyond document’s original size, update operation fail.
slightly more sophisticated answer
if field isn't mandatory, add new documents field , leave old documents are, using sensible default value documents not have field.
if need it
- stop reading , writing capped collection
- copy documents capped collection temporary collection
- change documents needed in temporary collection
- drop , recreate capped collection
- read documents temporary collection in desired order , insert them recreated capped collection.
after did "1.", can use "2." on shell:
var bulk = db.temp.initializeorderedbulkop(); var counter = 0; db.capped.find().foreach( function(doc){ bulk.insert(doc); // in case have lot of documents in // capped collection, makes sense // intermediate execute if( ++counter % 10000 == 0){ bulk.execute(); bulk = db.temp.initializeorderedbulkop(); } } ); // execute remainder bulk.execute() this should adaptable "5."
Comments
Post a Comment