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

  1. stop reading , writing capped collection
  2. copy documents capped collection temporary collection
  3. change documents needed in temporary collection
  4. drop , recreate capped collection
  5. 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

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -