@@ -1119,7 +1119,7 @@ Collection.prototype.isCapped = function(options, callback) {
1119
1119
/**
1120
1120
* Creates an index on the db and collection collection.
1121
1121
* @method
1122
- * @param {(string|object) } fieldOrSpec Defines the index.
1122
+ * @param {(string|array| object) } fieldOrSpec Defines the index.
1123
1123
* @param {object } [options] Optional settings.
1124
1124
* @param {(number|string) } [options.w] The write concern.
1125
1125
* @param {number } [options.wtimeout] The write concern timeout.
@@ -1138,6 +1138,25 @@ Collection.prototype.isCapped = function(options, callback) {
1138
1138
* @param {ClientSession } [options.session] optional session to use for this operation
1139
1139
* @param {Collection~resultCallback } [callback] The command result callback
1140
1140
* @return {Promise } returns Promise if no callback passed
1141
+ * @example
1142
+ * const collection = client.db('foo').collection('bar');
1143
+ *
1144
+ * await collection.createIndex({ a: 1, b: -1 });
1145
+ *
1146
+ * // Alternate syntax for { c: 1, d: -1 } that ensures order of indexes
1147
+ * await collection.createIndex([ [c, 1], [d, -1] ]);
1148
+ *
1149
+ * // Equivalent to { e: 1 }
1150
+ * await collection.createIndex('e');
1151
+ *
1152
+ * // Equivalent to { f: 1, g: 1 }
1153
+ * await collection.createIndex(['f', 'g'])
1154
+ *
1155
+ * // Equivalent to { h: 1, i: -1 }
1156
+ * await collection.createIndex([ { h: 1 }, { i: -1 } ]);
1157
+ *
1158
+ * // Equivalent to { j: 1, k: -1, l: 2d }
1159
+ * await collection.createIndex(['j', ['k', -1], { l: '2d' }])
1141
1160
*/
1142
1161
Collection . prototype . createIndex = function ( fieldOrSpec , options , callback ) {
1143
1162
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
@@ -1153,16 +1172,43 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
1153
1172
return executeOperation ( this . s . topology , createIndexOperation , callback ) ;
1154
1173
} ;
1155
1174
1175
+ /**
1176
+ * @typedef {object } Collection~IndexDefinition
1177
+ * @description A definition for an index. Used by the createIndex command.
1178
+ * @see https://docs.mongodb.com/manual/reference/command/createIndexes/
1179
+ */
1180
+
1156
1181
/**
1157
1182
* Creates multiple indexes in the collection, this method is only supported for
1158
1183
* MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported
1159
- * error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
1184
+ * error.
1185
+ *
1186
+ * **Note**: Unlike {@link Collection#createIndex createIndex}, this function takes in raw index specifications.
1187
+ * Index specifications are defined {@link http://docs.mongodb.org/manual/reference/command/createIndexes/ here}.
1188
+ *
1160
1189
* @method
1161
- * @param {array } indexSpecs An array of index specifications to be created
1190
+ * @param {Collection~IndexDefinition[] } indexSpecs An array of index specifications to be created
1162
1191
* @param {Object } [options] Optional settings
1163
1192
* @param {ClientSession } [options.session] optional session to use for this operation
1164
1193
* @param {Collection~resultCallback } [callback] The command result callback
1165
1194
* @return {Promise } returns Promise if no callback passed
1195
+ * @example
1196
+ * const collection = client.db('foo').collection('bar');
1197
+ * await collection.createIndexes([
1198
+ * // Simple index on field fizz
1199
+ * {
1200
+ * key: { fizz: 1 },
1201
+ * }
1202
+ * // wildcard index
1203
+ * {
1204
+ * key: { '$**': 1 }
1205
+ * },
1206
+ * // named index on darmok and jalad
1207
+ * {
1208
+ * key: { darmok: 1, jalad: -1 }
1209
+ * name: 'tanagra'
1210
+ * }
1211
+ * ]);
1166
1212
*/
1167
1213
Collection . prototype . createIndexes = function ( indexSpecs , options , callback ) {
1168
1214
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
0 commit comments