How do I get Strapi to make Mongo / Mongoose index the "id" column?

Viewed 1296

It seems that there is no way (that I can see) to tell Strapi to "make" Mongo index the id field of my thing collection.

The way I know it's not indexed is by issuing the following command directly in 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"
}

My thing schema looks as follows:

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

Note that the Mongo-internal field _id is properly indexed, meaning the following command, to give me all records of the collection in _id reverse sort order, works:

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

So _id is indexed, but id is not, and I don't see an obvious way in the schema definition to "force" it.

What I have tried: I added to the schema definition my own id attribute definition like so:

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

Very clever, right? But Strapi didn't like it one bit... refuses to start with:

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

How do I "suggest" to Strapi to please index the reserved attribute id so that my queries become fast?

As a little bit of background, I migrated my Strapi and database from 3.0.0 alpha recently. So perhaps somewhere during that transition, the indexing was missed. But there has to be a way to "tell" Strapi what to index and what to not index, including reserved attributes, doesn't there?

Mongo Version: 4.2.3

Strapi Version: 3.0.5

1 Answers

Turns out I was chasing a ghost. The id column is virtual in Strapi. In Mongo, if that is your underlying database, it is physically stored as _id and only mapped to id in queries and query results. And it is always indexed. So, never mind...

Related