parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Index on default field not created correctly

Open sunshineo opened this issue 5 years ago • 7 comments

  • [x] This isn't a vulnerability disclosure, if it is please follow our security policy.

  • [x] You're running version >=2.3.2 of Parse Server, we can't accept issues for very outdated releases, please update to a newer version.

  • [x] This isn't a question, if you need if you have questions about code please use Stack Overflow with the parse-platform tag & other questions can be posted on the community forum.

  • [x] You've searched through existing issues, your issue may have been reported or resolved before.

Discussion started on https://community.parseplatform.org/t/how-to-add-index-on-createdat-and-updatedat-if-possible/586/5 @davimacedo and I feel this is a bug

We tried to add an index to createdAt by post to /schemas/${className}

    indexes: {
            _createdAt_: {
                createdAt: 1
            },
            _updatedAt_: {
                updatedAt: 1
            }
        }

The post worked. Looking at the MongoDB it seems the indexes are created but on MongoDB it is _created_at and _updated_at. So the index does not work.

image

We tried to do the following but got an error

{
    indexes: {
            _created_at_: {
                _created_at: 1
            },
            _updated_at_: {
                _updated_at: 1
            }
        }
}
{
    “code”: 102,
    “error”: “Field _created_at does not exist, cannot add index.”
}

Environment Setup

  • Server

    • parse-server version (Be specific! Don't say 'latest'.) : 2.11.0
    • Operating System: Mac
    • Hardware: Mac
    • Localhost or remote server? Localhost
  • Database

    • MongoDB version: 4.2.8
    • Localhost or remote server? Mongo Atlas

sunshineo avatar Jul 27 '20 17:07 sunshineo

Thank you for reporting.

@davimacedo If I may, I tag you here so you can follow along the continued conversation from the Community Forum.

mtrezza avatar Jul 27 '20 22:07 mtrezza

It seems that the test case expects the result you describe:

https://github.com/parse-community/parse-server/blob/292bdb713a1557510b01986b1dd9d2d69efcf4ed/spec/schemas.spec.js#L2931-L2942

The correct expected result should be an index { _created_at: 1 }, unless we transform the indices from internal to non-internal form in the response.

Parse Server internal field names are rarely exposed and for example in the dashboard displayed in the non-internal form. So to not confuse the developer I suggest that the index request should also be the non-internal field name (createdAt instead of the internal form _created_at). It is also the form that has been tried intuitively first, according to the post.

I will add a PR that transforms the index field names from non-internal to internal form, so the correct request will be:

indexes: {
    _createdAt_: {
        createdAt: 1
    },
    _updatedAt_: {
        updatedAt: 1
    }
}

mtrezza avatar Jul 28 '20 00:07 mtrezza

May i ask, if i create indexes directly in mongo console or mongo compass, it will work right?

uzaysan avatar Aug 08 '20 11:08 uzaysan

Yes. Whether an index is used for a query is determined by the MongoDB query planner (unless otherwise specified), so once an index exists in MongoDB, it will be available to the query planner.

mtrezza avatar Aug 10 '20 11:08 mtrezza

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 08 '20 02:11 stale[bot]

would love to have this issue fixed :/

guiquintelas avatar Oct 07 '22 19:10 guiquintelas

export const ArticleFm = {
    className: "ArticleFm",
    fields: {
        image: {type: "Pointer", targetClass: "AVFile"},
        name: {type: "String"},
        size: {type: "Number"},
        type: {type: "Number"},
    },
    indexes: {
        ArticleFm_createdAt_Index: {_created_at: 1},
    },
    classLevelPermissions: {
        find: {},
        count: {},
        get: {},
        update: {},
        create: {},
        delete: {},
        protectedFields: {},
    },
}
[TS] error: Failed to run migrations: Error during update of schema for type ArticleFm: ParseError: 102 Field _created_at does not exist, cannot add index.

badboy-tian avatar Dec 18 '23 02:12 badboy-tian