Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key order in schema definition is reversed #1277

Closed
2 of 4 tasks
cristian-moreno-ruiz opened this issue Mar 10, 2022 · 1 comment
Closed
2 of 4 tasks

Key order in schema definition is reversed #1277

cristian-moreno-ruiz opened this issue Mar 10, 2022 · 1 comment
Labels

Comments

@cristian-moreno-ruiz
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am currently integrating nestjs/mongoose into our project and I was expecting Documents to retain the key order as defined in the schema, but instead, key order is reversed. Some time ago, mongoose was doing this on purpose, for some performance optimisation reason, and a retainKeyOrder schema option existed to prevent that.

Since version 5, this behaviour was removed from mongoose (https://mongoosejs.com/docs/migrating_to_5.html#retain-key-order), but I am experiencing it when I define the schema through decorators with @nestjs/mongoose.

Is there any known reason for this?

Minimum reproduction code

https://gist.github.com/cristian-moreno-ruiz/abcfe808067ecd894b8ed328606d97f2

Steps to reproduce

  1. Insert a document using mongoose: await this.accountModel.insertMany(account);
  2. Retrieving the document (or inspecting through a Mongo client) will return the document with reverse ordered keys.

Expected behavior

I would expect to get the document with original key order:

  {
    first: 'first',
    second: 'second',
    _id: new ObjectId("622a42bb465c46996faa5e35"),
    __v: 0
  }

But I get:

  {
    second: 'second',
    first: 'first',
    _id: new ObjectId("622a42bb465c46996faa5e35"),
    __v: 0
  }

Package version

9.0.2

mongoose version

6.2.5

NestJS version

8.2.5

Node.js version

14.16.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Let's track this here #1278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants