node.js - 「 'expireAfterSeconds' 选项仅在 '_ts' 字段上受支持。 」 显示错误

标签 node.js azure-cosmosdb azure-cosmosdb-mongoapi

我在 node.js 中使用 cosmos db 进行 session 存储。宇宙数据库版本是 3.6 。

我执行以下代码。

const expressSession = require("express-session");
const MongoStore = require("connect-mongo")(expressSession);
const store = new MongoStore({
        mongooseConnection: mongoose.connection,
        ttl:24 * 60 * 60 * 1000,
})

结果,显示以下消息。
  (node:16068) UnhandledPromiseRejectionWarning: MongoError: The 'expireAfterSeconds' option is supported on '_ts' field only.

这个问题的解决方案是什么?

最佳答案

CosmosDB 是与 MongoDB 不同的服务器实现,并且某些功能和行为有所不同。

Cosmos 目前仅支持 Cosmos 的 internal modification timestamp field _ts 上的 TTL 索引:

_ts is a Cosmos DB-specific field and is not accessible from MongoDB clients. It is a reserved (system) property that contains the timestamp of the document's last modification.



由于 connect-mongo 使用名为 expires 的字段作为 ttl 值,因此默认情况下它不适用于 Cosmos。

但是,您可以通过使用 connect-mongocompatibility mode 来解决此问题,该方法在 Node 应用程序中使用效率较低的基于计时器的方法,而不是 MongoDB 服务器支持的 native TTL 索引:
const store = new MongoStore({
        mongooseConnection: mongoose.connection,
        ttl:24 * 60 * 60 * 1000,
        autoRemove: 'interval',
        autoRemoveInterval: 10 // Value in minutes (default is 10)
})

您可以使用 autoRemoveInterval 选项调整计时器间隔,该选项设置运行查询以删除过期文档的频率。

关于node.js - 「 'expireAfterSeconds' 选项仅在 '_ts' 字段上受支持。 」 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638751/

相关文章:

Azure Cosmos 数据库批量导入

node.js - CosmosDB MongoDB 适配器投影不起作用

node.js - Azure Function Runtime 版本 : 2. 0.11888.0(测试版)变得无法连接到 cosmosDB

node.js - 尽管项目在 docker 容器上,但如何在我的 IDE 上提供 linting 功能?

javascript - Sequelize,如何更新另一个模型上的关联模型

node.js - (node.js 模块)sharp 图像处理器保持源文件打开,调整大小后无法取消链接原始文件

c# - 我可以通过附近的多边形查询带有边界多边形的 DocumentDB 文档吗?

c# - 如何使用 CreateDocumentAsync 将 json 内容存储到 CosmosDB

azure - 构建管道中的 Azure CosmosDb 模拟器任务所需的 MongoDb 连接字符串是什么?

javascript - 让 HTML 表单使用 JSON API