尽管 Cloudant 保留每个文档的修订历史记录,但不应将其用作版本控制系统,因为旧修订的主体会在压缩期间定期删除。
一种方法是在您的数据库中采用“只写”模式。假设您的文档如下所示:
{
"_id": "<autogeneratedid>",
"_rev: "<autogeneratedrev>",
"ref": "abc123",
"ts": 1464074759315,
"status": "provisional",
"name": "the name",
"body": "## document body"
}
每次您想要创建文档“abc123”的新版本时,只需将新版本插入到具有新时间戳 (ts) 的同一数据库中即可。
{
"_id": "<autogeneratedid>",
"_rev: "<autogeneratedrev>",
"ref": "abc123",
"ts": 1464074866595,
"status": "live",
"name": "the new name",
"body": "## new document body"
}
然后我们可以在 Cloudant 中创建一个secondary index,它允许我们检索您文档的最新版本:
function(doc) {
emit([doc.ref, doc.ts], null);
}
这会创建一个键为[doc.ref, doc.ts]的视图,所以当我们想要获取文档的最新版本时,我们可以查询这个视图:
GET <cloudant account>/mydb/_design/mydesigndoc/_view/myview?endkey=["abc123"]&startkey=["abc123z"]&descending=true&limit=1&include_docs=true
获取具有最新时间戳的版本。