@@ -981,23 +981,21 @@ export default class Collection extends ShellApiWithMongoClass {
981
981
}
982
982
) ;
983
983
}
984
-
985
984
/**
986
- * Create indexes for a collection
987
- *
985
+ * Internal function which calls the Service Provider createIndexes function.
986
+ * This function is used also by createIndex and ensureIndex
987
+ *
988
988
* @param {Document } keyPatterns - An array of documents that contains
989
989
* the field and value pairs where the field is the index key and the
990
990
* value describes the type of index for that field.
991
991
* @param {Document } options - createIndexes options (
992
992
* name, background, sparse ...)
993
993
* @return {Promise }
994
994
*/
995
- @returnsPromise
996
- @serverVersions ( [ '3.2.0' , ServerVersions . latest ] )
997
- @apiVersions ( [ 1 ] )
998
- async createIndexes (
995
+ async _createIndexes (
999
996
keyPatterns : Document [ ] ,
1000
- options : CreateIndexesOptions = { }
997
+ options : CreateIndexesOptions = { } ,
998
+ commitQuorum ?: number | string
1001
999
) : Promise < string [ ] > {
1002
1000
assertArgsDefinedType ( [ keyPatterns ] , [ true ] , 'Collection.createIndexes' ) ;
1003
1001
if ( typeof options !== 'object' || Array . isArray ( options ) ) {
@@ -1006,15 +1004,39 @@ export default class Collection extends ShellApiWithMongoClass {
1006
1004
CommonErrors . InvalidArgument
1007
1005
) ;
1008
1006
}
1009
-
1010
1007
const specs = keyPatterns . map ( ( pattern ) => ( {
1011
1008
...options , key : pattern
1012
1009
} ) ) ;
1013
-
1014
- this . _emitCollectionApiCall ( 'createIndexes' , { specs } ) ;
1015
-
1010
+ const createIndexesOptions : CreateIndexesOptions = { ...await this . _database . _baseOptions ( ) , ...options } ;
1011
+ if ( undefined !== commitQuorum ) {
1012
+ createIndexesOptions . commitQuorum = commitQuorum ;
1013
+ }
1016
1014
return await this . _mongo . _serviceProvider . createIndexes (
1017
- this . _database . _name , this . _name , specs , { ...await this . _database . _baseOptions ( ) , ...options } ) ;
1015
+ this . _database . _name , this . _name , specs , createIndexesOptions ) ;
1016
+ }
1017
+ /**
1018
+ * Create indexes for a collection
1019
+ *
1020
+ * @param {Document } keyPatterns - An array of documents that contains
1021
+ * the field and value pairs where the field is the index key and the
1022
+ * value describes the type of index for that field.
1023
+ * @param {Document } options - createIndexes options (
1024
+ * name, background, sparse ...)
1025
+ * @return {Promise }
1026
+ */
1027
+ @returnsPromise
1028
+ @serverVersions ( [ '3.2.0' , ServerVersions . latest ] )
1029
+ @apiVersions ( [ 1 ] )
1030
+ async createIndexes (
1031
+ keyPatterns : Document [ ] ,
1032
+ options : CreateIndexesOptions = { } ,
1033
+ commitQuorum ?: number | string
1034
+ ) : Promise < string [ ] > {
1035
+ const specs = keyPatterns . map ( ( pattern ) => ( {
1036
+ ...options , key : pattern
1037
+ } ) ) ;
1038
+ this . _emitCollectionApiCall ( 'createIndexes' , { specs } ) ;
1039
+ return this . _createIndexes ( keyPatterns , options , commitQuorum )
1018
1040
}
1019
1041
1020
1042
/**
@@ -1032,7 +1054,8 @@ export default class Collection extends ShellApiWithMongoClass {
1032
1054
@apiVersions ( [ 1 ] )
1033
1055
async createIndex (
1034
1056
keys : Document ,
1035
- options : CreateIndexesOptions = { }
1057
+ options : CreateIndexesOptions = { } ,
1058
+ commitQuorum ?: number | string
1036
1059
) : Promise < string > {
1037
1060
assertArgsDefinedType ( [ keys ] , [ true ] , 'Collection.createIndex' ) ;
1038
1061
if ( typeof options !== 'object' || Array . isArray ( options ) ) {
@@ -1042,10 +1065,7 @@ export default class Collection extends ShellApiWithMongoClass {
1042
1065
) ;
1043
1066
}
1044
1067
this . _emitCollectionApiCall ( 'createIndex' , { keys, options } ) ;
1045
-
1046
- const spec = { key : keys , ...options } ; // keep options for java
1047
- const names = await this . _mongo . _serviceProvider . createIndexes (
1048
- this . _database . _name , this . _name , [ spec ] , { ...await this . _database . _baseOptions ( ) , ...options } ) ;
1068
+ const names = await this . _createIndexes ( [ keys ] , options , commitQuorum ) ;
1049
1069
if ( ! Array . isArray ( names ) || names . length !== 1 ) {
1050
1070
throw new MongoshInternalError (
1051
1071
`Expected createIndexes() to return array of length 1, saw ${ names } ` ) ;
@@ -1068,19 +1088,11 @@ export default class Collection extends ShellApiWithMongoClass {
1068
1088
@apiVersions ( [ 1 ] )
1069
1089
async ensureIndex (
1070
1090
keys : Document ,
1071
- options : CreateIndexesOptions = { }
1091
+ options : CreateIndexesOptions = { } ,
1092
+ commitQuorum ?: number | string
1072
1093
) : Promise < Document > {
1073
- assertArgsDefinedType ( [ keys ] , [ true ] , 'Collection.ensureIndex' ) ;
1074
- if ( typeof options !== 'object' || Array . isArray ( options ) ) {
1075
- throw new MongoshInvalidInputError (
1076
- 'The "options" argument must be an object.' ,
1077
- CommonErrors . InvalidArgument
1078
- ) ;
1079
- }
1080
1094
this . _emitCollectionApiCall ( 'ensureIndex' , { keys, options } ) ;
1081
-
1082
- const spec = { key : keys , ...options } ;
1083
- return await this . _mongo . _serviceProvider . createIndexes ( this . _database . _name , this . _name , [ spec ] , { ...await this . _database . _baseOptions ( ) , ...options } ) ;
1095
+ return await this . _createIndexes ( [ keys ] , options , commitQuorum ) ;
1084
1096
}
1085
1097
1086
1098
/**
0 commit comments