No, MongoDB does not store versions of documents. There is only the "current" version of the document as far as the database API is concerned.
It would be necessary to create your own scheme if you wanted such a thing. You could use a version
field in your document and use $inc
for example to increment it as needed and also verify that the version
value matched before applying an update.
Example:
db.myCollection.update(
{ _id: "abc123", _version: 5 },
{
$set: { 'fieldA' : 'some-value' },
$inc: { '_version' : 1 }
}
)
The above example would find a document with the specific _id
and _version
fields. If matched, a field called fieldA
is set to a new value and the _version
field is incremented by 1
. If another update was attempted on the same document and version, it would fail as the _version
would have been updated to 6
.