通过NodeJS获取最后创建的文档Cloudant/CouchDB

Getting the last created document Cloudant/CouchDB thorugh NodeJS

我想通过 NodeJS 中的 Cloudant npm 模块获取数据库中最后插入的文档,但我没有在文档中找到任何有用的信息。

有没有办法直接从 db.get 文档函数执行此操作?我在数据库文档上没有时间戳键,所以我也不能手动排序。

Cloudant/CouchDB 不会为文档添加时间戳,因此它没有 "last created" 的概念。这给您留下了您提到的两个客户端解决方案:

  1. 在创建文档时将时间戳添加到 JSON 中。例如

    { "username": "glynn", "timestamp": 1525245671509, "level": 4 }

然后您可以 query the database or create a MapReduce view 按时间戳对文档进行排序。

  1. 如果您的应用程序允许,您的文档 _id 字段可以以时间戳开头,例如1525245671509-user:glynn。这将允许您查询 _all_docs 端点以获取最后插入的文档,例如

GET /db/_all_docs?descending=true&limit=1&include_docs=true