【问题标题】:How do I get Strapi to make Mongo / Mongoose index the "id" column?如何让 Strapi 使 Mongo / Mongoose 索引成为“id”列?
【发布时间】:2020-09-05 03:08:11
【问题描述】:

似乎没有办法(我可以看到)告诉 Strapi “制作” Mongo 索引我的 thing 集合的 id 字段。

我知道它没有被索引的方法是直接在 Mongo 中发出以下命令:

> db.thing.find().sort({id:-1})
Error: error: {
    "ok" : 0,
    "errmsg" : "Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
    "code" : 96,
    "codeName" : "OperationFailed"
}

我的事物架构如下所示:

{
    "kind": "collectionType",
    "connection": "default",
    "collectionName": "",
    "info": {
        "name": "thing",
        "description": ""
    },
    "options": {
        "timestamps": true
    },
    "attributes": {
        "data": {
            "required": true,
            "type": "json"
        },
    }
}

请注意,Mongo 内部字段 _id 已正确索引,这意味着以下命令以 _id 反向排序顺序为我提供集合的所有记录,有效:

> db.thing.find().sort({_id:-1})

所以_id 已编入索引,但id 未编入索引,而且我在架构定义中看不到“强制”它的明显方式。

我尝试过的:我将自己的id 属性定义添加到架构定义中,如下所示:

"attributes": {
    "id": {
       "index": true
    },
    "data": {
       ...
    }

非常聪明,对吧?但是 Strapi 一点也不喜欢……拒绝开始:

error Model "thing" is using reserved attribute names "id".

我如何“建议”Strapi 索引保留属性id 以便我的查询变得更快?

作为一点背景知识,我最近从 3.0.0 alpha 迁移了我的 Strapi 和数据库。因此,也许在过渡期间的某个地方,索引被遗漏了。但是必须有一种方法来“告诉”Strapi 索引什么和不索引什么,包括保留属性,不是吗?

Mongo 版本:4.2.3

Strapi 版本:3.0.5

【问题讨论】:

    标签: node.js mongodb mongoose strapi


    【解决方案1】:

    原来我在追一个鬼。 id 列在 Strapi 中是虚拟的。在 Mongo 中,如果那是您的底层数据库,它在物理上存储为 _id,并且仅在查询和查询结果中映射到 id。它总是被索引的。所以,没关系...?

    【讨论】:

      猜你喜欢
      • 2015-04-28
      • 2020-09-26
      • 2016-03-30
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      相关资源
      最近更新 更多