diff --git a/docs/docs/usage-with-mongo.md b/docs/docs/usage-with-mongo.md index 045fb81dac69..6c41fe3926ec 100644 --- a/docs/docs/usage-with-mongo.md +++ b/docs/docs/usage-with-mongo.md @@ -120,17 +120,32 @@ Alternatively you can call `ensureIndexes()` method on the `SchemaGenerator`: await orm.getSchemaGenerator().ensureIndexes(); ``` -> You can pass additional index/unique options via `options` parameter: -> -> `@Unique({ options: { partialFilterExpression: { name: { $exists: true } } }})` +You can pass additional index/unique options via `options` parameter: -> You can also create text indexes by passing `type` parameter: -> -> `@Index({ properties: ['name', 'caption'], type: 'text' })` +```ts +@Unique({ options: { partialFilterExpression: { name: { $exists: true } } }}) +``` -> If you provide only `options` in the index definition, it will be used as is, this allows to define any kind of index: -> -> `@Index({ options: { point: '2dsphere', title: -1 } })` +You can also create text indexes by passing `type` parameter: + +```ts +@Index({ properties: ['name', 'caption'], type: 'text' }) +``` + +If you provide only `options` in the index definition, it will be used as is, this allows to define any kind of index: + +```ts +@Index({ options: { point: '2dsphere', title: -1 } }) +``` + +To set index weights, you can pass a tuple to the `options` parameter: + +```ts +@Index({ options: [ + { title: 'text', perex: 'text', key: 1 }, + { weights: { title: 10, perex: 5 } }, +] }) +``` ## Native collection methods diff --git a/packages/mongodb/src/MongoSchemaGenerator.ts b/packages/mongodb/src/MongoSchemaGenerator.ts index fd7ba178fac9..f21fd3a69574 100644 --- a/packages/mongodb/src/MongoSchemaGenerator.ts +++ b/packages/mongodb/src/MongoSchemaGenerator.ts @@ -142,6 +142,10 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator { const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames)); const collection = this.connection.getCollection(meta.name!); + if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) { + return res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]); + } + if (index.options && properties.length === 0) { return res.push([collection.collectionName, collection.createIndex(index.options)]); } diff --git a/tests/entities/FooBar.ts b/tests/entities/FooBar.ts index 6007248223c0..6ae48d291602 100644 --- a/tests/entities/FooBar.ts +++ b/tests/entities/FooBar.ts @@ -1,8 +1,21 @@ import { ObjectId } from 'bson'; -import { ArrayType, Entity, JsonType, OneToOne, PrimaryKey, Property, SerializedPrimaryKey } from '@mikro-orm/core'; +import { + ArrayType, + Entity, + Index, + JsonType, + OneToOne, + PrimaryKey, + Property, + SerializedPrimaryKey, +} from '@mikro-orm/core'; import { FooBaz } from './FooBaz'; @Entity() +@Index({ options: [ + { name: 'text', str: 'text', baz: 1 }, + { weights: { name: 10, str: 5 } }, +] }) export default class FooBar { @PrimaryKey()