@@ -24,7 +24,6 @@ import type {
24
24
} from './mongo_types' ;
25
25
import type { AggregateOptions } from './operations/aggregate' ;
26
26
import { BulkWriteOperation } from './operations/bulk_write' ;
27
- import type { IndexInformationOptions } from './operations/common_functions' ;
28
27
import { CountOperation , type CountOptions } from './operations/count' ;
29
28
import { CountDocumentsOperation , type CountDocumentsOptions } from './operations/count_documents' ;
30
29
import {
@@ -49,19 +48,16 @@ import {
49
48
FindOneAndUpdateOperation ,
50
49
type FindOneAndUpdateOptions
51
50
} from './operations/find_and_modify' ;
52
- import {
53
- CreateIndexesOperation ,
54
- type CreateIndexesOptions ,
55
- CreateIndexOperation ,
56
- type DropIndexesOptions ,
57
- DropIndexOperation ,
58
- type IndexDescription ,
59
- IndexesOperation ,
60
- IndexExistsOperation ,
61
- IndexInformationOperation ,
62
- type IndexSpecification ,
63
- type ListIndexesOptions
51
+ import type {
52
+ CreateIndexesOptions ,
53
+ DropIndexesOptions ,
54
+ IndexDescription ,
55
+ IndexDirection ,
56
+ IndexInformationOptions ,
57
+ IndexSpecification ,
58
+ ListIndexesOptions
64
59
} from './operations/indexes' ;
60
+ import { CreateIndexesOperation , DropIndexOperation } from './operations/indexes' ;
65
61
import {
66
62
InsertManyOperation ,
67
63
type InsertManyResult ,
@@ -575,15 +571,17 @@ export class Collection<TSchema extends Document = Document> {
575
571
indexSpec : IndexSpecification ,
576
572
options ?: CreateIndexesOptions
577
573
) : Promise < string > {
578
- return executeOperation (
574
+ const indexes = await executeOperation (
579
575
this . client ,
580
- new CreateIndexOperation (
581
- this as TODO_NODE_3286 ,
576
+ CreateIndexesOperation . fromIndexSpecification (
577
+ this ,
582
578
this . collectionName ,
583
579
indexSpec ,
584
580
resolveOptions ( this , options )
585
581
)
586
582
) ;
583
+
584
+ return indexes [ 0 ] ;
587
585
}
588
586
589
587
/**
@@ -623,8 +621,8 @@ export class Collection<TSchema extends Document = Document> {
623
621
) : Promise < string [ ] > {
624
622
return executeOperation (
625
623
this . client ,
626
- new CreateIndexesOperation (
627
- this as TODO_NODE_3286 ,
624
+ CreateIndexesOperation . fromIndexDescriptionArray (
625
+ this ,
628
626
this . collectionName ,
629
627
indexSpecs ,
630
628
resolveOptions ( this , { ...options , maxTimeMS : undefined } )
@@ -680,14 +678,14 @@ export class Collection<TSchema extends Document = Document> {
680
678
* @param indexes - One or more index names to check.
681
679
* @param options - Optional settings for the command
682
680
*/
683
- async indexExists (
684
- indexes : string | string [ ] ,
685
- options ?: IndexInformationOptions
686
- ) : Promise < boolean > {
687
- return executeOperation (
688
- this . client ,
689
- new IndexExistsOperation ( this as TODO_NODE_3286 , indexes , resolveOptions ( this , options ) )
681
+ async indexExists ( indexes : string | string [ ] , options ?: ListIndexesOptions ) : Promise < boolean > {
682
+ const indexNames : string [ ] = Array . isArray ( indexes ) ? indexes : [ indexes ] ;
683
+ const allIndexes : Set < string > = new Set (
684
+ await this . listIndexes ( options )
685
+ . map ( ( { name } ) => name )
686
+ . toArray ( )
690
687
) ;
688
+ return indexNames . every ( name => allIndexes . has ( name ) ) ;
691
689
}
692
690
693
691
/**
@@ -696,10 +694,7 @@ export class Collection<TSchema extends Document = Document> {
696
694
* @param options - Optional settings for the command
697
695
*/
698
696
async indexInformation ( options ?: IndexInformationOptions ) : Promise < Document > {
699
- return executeOperation (
700
- this . client ,
701
- new IndexInformationOperation ( this . s . db , this . collectionName , resolveOptions ( this , options ) )
702
- ) ;
697
+ return this . indexes ( { ...options , full : options ?. full ?? false } ) ;
703
698
}
704
699
705
700
/**
@@ -804,10 +799,19 @@ export class Collection<TSchema extends Document = Document> {
804
799
* @param options - Optional settings for the command
805
800
*/
806
801
async indexes ( options ?: IndexInformationOptions ) : Promise < Document [ ] > {
807
- return executeOperation (
808
- this . client ,
809
- new IndexesOperation ( this as TODO_NODE_3286 , resolveOptions ( this , options ) )
810
- ) ;
802
+ const indexes = await this . listIndexes ( options ) . toArray ( ) ;
803
+ const full = options ?. full ?? true ;
804
+ if ( full ) {
805
+ return indexes ;
806
+ }
807
+
808
+ const object : Record <
809
+ string ,
810
+ Array < [ name : string , direction : IndexDirection ] >
811
+ > = Object . fromEntries ( indexes . map ( ( { name, key } ) => [ name , Object . entries ( key ) ] ) ) ;
812
+
813
+ // @ts -expect-error TODO(NODE-6029): fix return type of `indexes()` and `indexInformation()`
814
+ return object ;
811
815
}
812
816
813
817
/**
0 commit comments