Skip to content

Commit 0f5278d

Browse files
committed
docs(createIndex): better document createIndex(es) method and add examples
Adds documentation around the createIndex command that links to docs.mongodb.com entry for an index definition. Also adds an example showing multiple index definitions to createIndexes, and adds some examples to createIndex. Makes explicit that createIndexes uses different syntax from createIndex. Fixes NODE-2024
1 parent f6e854c commit 0f5278d

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

lib/collection.js

+49-3
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ Collection.prototype.isCapped = function(options, callback) {
11191119
/**
11201120
* Creates an index on the db and collection collection.
11211121
* @method
1122-
* @param {(string|object)} fieldOrSpec Defines the index.
1122+
* @param {(string|array|object)} fieldOrSpec Defines the index.
11231123
* @param {object} [options] Optional settings.
11241124
* @param {(number|string)} [options.w] The write concern.
11251125
* @param {number} [options.wtimeout] The write concern timeout.
@@ -1138,6 +1138,25 @@ Collection.prototype.isCapped = function(options, callback) {
11381138
* @param {ClientSession} [options.session] optional session to use for this operation
11391139
* @param {Collection~resultCallback} [callback] The command result callback
11401140
* @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' }])
11411160
*/
11421161
Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
11431162
if (typeof options === 'function') (callback = options), (options = {});
@@ -1153,16 +1172,43 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
11531172
return executeOperation(this.s.topology, createIndexOperation, callback);
11541173
};
11551174

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+
11561181
/**
11571182
* Creates multiple indexes in the collection, this method is only supported for
11581183
* 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+
*
11601189
* @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
11621191
* @param {Object} [options] Optional settings
11631192
* @param {ClientSession} [options.session] optional session to use for this operation
11641193
* @param {Collection~resultCallback} [callback] The command result callback
11651194
* @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+
* ]);
11661212
*/
11671213
Collection.prototype.createIndexes = function(indexSpecs, options, callback) {
11681214
if (typeof options === 'function') (callback = options), (options = {});

0 commit comments

Comments
 (0)