Insert copy of document into same document for versioning

47 views
Skip to first unread message

John Menke

unread,
Apr 12, 2016, 7:05:20 AM4/12/16
to mongodb-user
We have a requirement for versioning that expects prior versions of a document to be stored in a version array within the document. Is it possible to both update fields in a document and save a copy of the document itself in a single operation?

All I want to do is append to the version array i don't want to have to copy down to client and update.

Allan Bazinet

unread,
Apr 12, 2016, 10:31:36 AM4/12/16
to mongodb-user
It sounds as if the requirement here is that the document must contain, within the document itself, an array that contains all previous versions of the document, which would include the array.

While I can't think of any way to do this without reading the document, I think the key thing here is that you're looking at exponential growth there, which does get out of hand quite quickly.  For example, consider a relatively small, 1K document; even with zero growth, at 14 revisions, you're at the document maximum size limit.

In cases where we've needed something like that, we've used a separate collection to store the versions, with documents containing two fields, a 'link' field, with the oid of the originating document, and a 'data' field, with the original contents, so as to handle the unbounded requirement without needing to worry about hitting the maximum document size limit.  With an index on the link field, it's straightforward to use.  Some of the drivers, such as mongoose, have a 'populate' function that makes getting the current version of the document from the link field easy.

Rhys Campbell

unread,
Apr 13, 2016, 3:24:49 AM4/13/16
to mongodb-user
Maximum document size is 16MB https://docs.mongodb.org/manual/refere nce/limits/

John Menke

unread,
Apr 17, 2016, 8:25:43 AM4/17/16
to mongodb-user

looking at the update operators.  even though i can append to an array i am not sure i can get a reference to the current document inside an update.  I don't see an operator that give this reference.  Is there an operator that gives reference to current document in update?



Update Operators

Fields

NameDescription
$incIncrements the value of the field by the specified amount.
$mulMultiplies the value of the field by the specified amount.
$renameRenames a field.
$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$setSets the value of a field in a document.
$unsetRemoves the specified field from a document.
$minOnly updates the field if the specified value is less than the existing field value.
$maxOnly updates the field if the specified value is greater than the existing field value.
$currentDateSets the value of a field to current date, either as a Date or a Timestamp.

Array

Operators

NameDescription
$Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSetAdds elements to an array only if they do not already exist in the set.
$popRemoves the first or last item of an array.
$pullAllRemoves all matching values from an array.
$pullRemoves all array elements that match a specified query.
$pushAllDeprecated. Adds several items to an array.
$pushAdds an item to an array.

Modifiers

NameDescription
$eachModifies the $push and $addToSet operators to append multiple items for array updates.
$sliceModifies the $push operator to limit the size of updated arrays.
$sortModifies the $push operator to reorder documents stored in an array.
$positionModifies the $push operator to specify the position in the array to add elements.
Reply all
Reply to author
Forward
0 new messages