Skip to content

orderBy is not working with UnderscoreNamingStrategy in Mongodb #4313

@funduck

Description

@funduck

I use MongoDB with UnderscoreNamingStrategy
find and findOne with sort by camel case field do not work, sorting is ignored.

The problem seems to be exactly with Mongo driver.

To Reproduce
Steps to reproduce the behavior:

@Entity()
export class A {
  @PrimaryKey()
  _id = randomUUID();
  
  @Property()
  complexName!: string;
}

orm = await MikroORM.init({
  entities: [A],
  clientUrl: await initMongoReplSet('mikro-orm-test'),
  driver: MongoDriver,
  namingStrategy: UnderscoreNamingStrategy,
});
await orm.schema.clearDatabase()

const a = new A();
a.complexName = 'a';
const b = new A();
b.complexName = 'b';
await orm.em.persistAndFlush(a);
await orm.em.persistAndFlush(b);
orm.em.clear();

const data2 = await orm.em.find(A, {}, {
  orderBy: { complexName: 'desc' },
});
expect(data2[0].complexName).toBe('b');

Expected behavior
Keys in orderBy should be transformed according to naming strategy.

Versions

Dependency Version
node 18.14
typescript 4.9.5
mikro-orm 5
your-driver MongoDB

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions