Câu hỏi phỏng vấn NoSQL
Câu hỏi

Explain how would you keep docu...

Câu trả lời

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

Revision Tracking

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:

  1. 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].

  2. 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].

  3. 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].

Example:

{
  "_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

Document Versioning Pattern

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.

  1. 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].

  2. 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].

Example:

{
  "_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

senior

Gợi ý câu hỏi phỏng vấn

junior

Explain difference between scaling horizontally and vertically for databases

middle

What does Document-oriented vs. Key-Value mean in context of NoSQL?

senior

Explain use of transactions in NoSQL

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào