S
S
Stanislav2017-12-15 12:00:34
MongoDB
Stanislav, 2017-12-15 12:00:34

Why doesn't creating indexes in mongoose schema work?

I don’t understand what’s wrong, the created rule for adding indexes in the schema doesn’t work for some reason.
Start from the beginning
Delete all indexes in the popsts collection
db.posts.dropIndexes()
Result of db.posts.getIndexes()

[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "site.posts"
        }
]

Trying to select .skip(10000).limit(12).sort({indexAt: -1}) on the page the following message pops up
Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.

OK. there is no index, I go to the mongoose schema and add the following I
schema.index({indexAt: 1});
restart the node and refresh the page
Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.

I go to mongodb and check indexes
[
    {
            "v" : 2,
            "key" : {
                    "_id" : 1
            },
            "name" : "_id_",
            "ns" : "site.posts"
    },
    {
            "v" : 2,
            "unique" : true,
            "key" : {
                    "url" : 1
            },
            "name" : "url_1",
            "ns" : "site.posts",
            "background" : true
    },
    {
            "v" : 2,
            "key" : {
                    "indexAt" : 1
            },
            "name" : "indexAt_1",
            "ns" : "site.posts",
            "background" : true
    }
]

The index was created, but the error does not sell until the index is created in mongodb itself.
db.wallpapers.createIndex({'indexAt': 1})
And only after that everything starts working, and the structure of the indexes is the same. Why is that? Mongodb cache indexes or what's going on? How can this be overcome? (MongoDB shell version v3.4.10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Mishin, 2019-04-02
@Dangear

If still relevant, then this error occurs due to the fact that there is not enough memory for the sort () operation. To increase its limit to 128MB, you can try this method:

> db.adminCommand({"setParameter": 1, "internalQueryExecMaxBlockingSortBytes" : 134217728})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question