I created a capped collection to store my log data with few fields. Due to some requirement I wanted to add an additional field named "createAt" in this collection.
db.myLogs.update({},{$set: {"createAt":new Date()}})
This is throwing below error:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 10003,
"errmsg" : "Cannot change the size of a document in a capped collection: 39 != 57"
}
})
How can I add a few field into capped collection?
Simple answer
As mongod tells you, you can't. As does the documentation:
Slightly more sophisticated answer
If the field isn't mandatory, simply add the new documents with the field and leave the old documents as they are, using a sensible default value for the documents which do not have the field.
If you really need to do it
After you did "1.", you can use something like this for "2." on the shell:
This should be easily adaptable for "5."