Explain how would you keep docu...
Explain how would you keep docu...
To keep document change history in a NoSQL database, you can use various strategies depending on the specific NoSQL database you are using and your application's requirements. Here are some common approaches:
CouchDB inherently supports document revisions, but it is not designed as a version control system. Revisions are primarily used for conflict resolution in distributed environments. Here’s how you can manage document revisions in CouchDB:
Accessing Revisions: You can retrieve specific revisions of a document using the rev
parameter in your request. For example:
GET /db/doc_id?rev=revision_id
This will return the specified revision of the document[6][10].
Limitations: CouchDB does not guarantee the retention of old revisions indefinitely. Revisions can be removed during compaction or replication processes. Therefore, if you need to ensure access to previous versions, you should store them as separate documents[1][2].
Manual Versioning: If you need to keep a history of document changes, you can manually copy the old document content and re-insert it as a new document, excluding the rev_id
field[2].
{
"_id": "doc_id",
"name": "Document Name",
"type": "original",
"_rev": "1-abc"
}
When updating, you can save the previous version as a new document:
{
"_id": "doc_id_v1",
"name": "Document Name",
"type": "original",
"_rev": "1-abc"
}
MongoDB does not have built-in versioning, but you can implement document versioning using the Document Versioning Pattern. This involves storing historical versions of documents in a separate collection.
Separate Collections: Store the current version of the document in one collection and all historical versions in another collection. This approach ensures that queries on the current data remain efficient while still retaining a complete history[5][20].
Embedded Versions: Another approach is to embed version information directly within the document. Each time a document is updated, a new version is created, preserving the previous state. This can be done using an array to store the versions[9].
{
"_id": "doc_id",
"current_version": {
"version_number": 3,
"data": {
"name": "Document Name",
"type": "updated"
},
"updated_at": "2024-05-20T20:00:00Z"
},
"versions": [
{
"version_number": 2,
"data": {
"name": "Document Name",
"type": "intermediate"
},
"updated_at": "2024-05-19T20:00:00Z"
},
{
"version_number": 1,
"data": {
"name": "Document Name",
"type": "...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào