0
db.FirstTry.update({"id":1},{$set:{"Name":"Selvah"}});

I am getting an error like this

{ "message" : "WriteError({'code':10003,'index':0,'errmsg':'Cannot change the size of a document in a capped collection: 72 != 71','op':{'q':{'id':1},'u':{'$set':{'Name':'Selvah'}},'multi':false,'upsert':false}})", "stack" : "script:1:55" + "script:1:13" }

mickl
  • 48,568
  • 9
  • 60
  • 89
Selvah
  • 25
  • 5

2 Answers2

2

The size of the document in a capped collection is fixed, so you cannot insert a new field into the document. You will have to create a new temp collection and copy all data from old collection to new collection and add new field.

Something like this:

db.createCollection( "logItems_temp", { capped: true, size: 100000 } )
var cur = db.logItems.find()
while (cur.hasNext()) {
  logItem = cur.next(); 
  if(logItem.name == null){
      logItem.name = selvah;
  }
  db.logItems_temp.insert(logItem);
}
db.logItems.drop()
db.logItems_temp.renameCollection("logItems")
Anirudh Bagri
  • 2,346
  • 1
  • 21
  • 33
  • Is there any way to insert new fields use the update command? If possible can u just tell me – Selvah Dec 15 '17 at 07:33
  • In a capped collection you cannot insert new fields as it will increase the size of the document. check official document here: https://docs.mongodb.com/manual/core/capped-collections/#document-size – Anirudh Bagri Dec 15 '17 at 07:52
  • But if you really want to do it, you'll have to copy documents to a new collection, update them and then create a new capped collection – Anirudh Bagri Dec 15 '17 at 07:54
  • yeah bro i got tat :-) – Selvah Dec 15 '17 at 08:14
0

I think this solves your Problem http://www.solidsyntax.be/2016/03/26/MongoDB-Cannot-change-the-size-of-a-document-in-a-capped-collection/

The official documentation lists Information about working with Capped collections: https://docs.mongodb.com/manual/core/capped-collections/#restrictions-and-recommendations

rieckpil
  • 10,470
  • 3
  • 32
  • 56