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

Indexes in sub-document for mongodb #2129

Closed
anlauren opened this issue Aug 16, 2021 · 6 comments
Closed

Indexes in sub-document for mongodb #2129

anlauren opened this issue Aug 16, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request mongodb
Milestone

Comments

@anlauren
Copy link

Describe the bug
It seems that mikro-orm mondodb driver doesn't support sub-document indexing. That makes sense for SQL but it's common practice in mongodb. It looks like it's not possible to create an index like myField.mySubField.
Now it could be because of how i've written the embedding that's it's not working.
Stack trace

[Nest] 79737   - 08/16/2021, 11:19:23 AM   [ExceptionHandler] Entity Test has wrong index definition: 'testEmbedded.a' does not exist. You need to use property name, not column name. +47ms
MetadataError: Entity Test has wrong index definition: 'testEmbedded.a' does not exist. You need to use property name, not column name.
    at Function.unknownIndexProperty (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/errors.js:126:16)
    at MetadataValidator.validateIndexes (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/metadata/MetadataValidator.js:109:50)
    at MetadataValidator.validateEntityDefinition (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/metadata/MetadataValidator.js:26:14)
    at MetadataDiscovery.processEntity (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:297:24)
    at filtered.forEach.meta (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:52:63)
    at Array.forEach (<anonymous>)
    at MetadataDiscovery.discover (/Users/antoinelaurent/virtual-event-service/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:52:18)
    at process._tickCallback (internal/process/next_tick.js:68:7)

To Reproduce
Steps to reproduce the behavior:

@Embeddable()
export class TestEmbedded {
  @Property()
  a: number;
}

@Entity({ tableName: 'test' })
@Index({ properties: ['testEmbedded.a'] })
export class Test extends BaseEntity {
  @Embedded({
    entity: () => TestEmbedded,
    object: true,
  })
  testEmbedded: TestEmbedded;
}

Expected behavior
Should be able to use a . notation in mongodb indexes

Additional context
My original problem arises when i tried to create a compound index with a sub-field and a field of a document.

Versions

Dependency Version
node v10.22.0
typescript v4.0.5
mikro-orm v4.3.4
@mikro-orm/mongodb v4.3.4
@B4nan
Copy link
Member

B4nan commented Aug 16, 2021

Indeed, this is not supported -> this is a feature request, not a bug report.

@andrey-hohlov
Copy link

Hi, @B4nan
Any plans on it?

It works for @Index decorator :

@Index({ options: { 'embedded.property': 1 } })

But nothing works for @Unique.

@B4nan
Copy link
Member

B4nan commented Aug 3, 2022

Fastest way to see this happen is sending a PR 🤷

@bebjakub
Copy link

bebjakub commented Aug 4, 2023

@andrey-hohlov This works for a unique index, also with a sparse index if needed...

@Property({ nullable: true, unique: true })

more #4495

@B4nan
Copy link
Member

B4nan commented Aug 4, 2023

Interesting, maybe this got fixed by some unrelated change over time. Will keep this open till we have some explicit tests for it so we can be sure it actually works (and it will remain working over time as well)

@B4nan
Copy link
Member

B4nan commented Aug 26, 2023

Okay, this apparently works as expected, not sure what fixed it. Added tests to ensure it really works, will mark it as a feature commit so it gets into the changelog, although it will just add the test.

@B4nan B4nan closed this as completed in 040896e Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mongodb
Projects
None yet
Development

No branches or pull requests

4 participants