============== Create Indexes ============== To create an index on a field or fields, pass an index specification document to the ``createIndex()`` method: .. code-block:: js { : , : ... } Create an Ascending Index ------------------------- For an ascending index type, specify ``1`` for ````. The following example creates an ascending index key for the ``dateOfBirth`` field: .. code-block:: js async function createAscendingIndex(db) { // Get the users collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ dateOfBirth : 1 }); console.log(result); return result; }; Create a Descending Index ------------------------- For an descending index type, specify ``-1`` for ````. The following example specifies a descending index key on the ``lastName`` field: .. code-block:: js async function createDescendingIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ lastName : -1 }); console.log(result); return result; }; Create a Compound Index ----------------------- To specify a compound index, use the ``compoundIndex`` method. The following example specifies a compound index key composed of the ``lastName`` field sorted in descending order, followed by the ``dateOfBirth`` field sorted in ascending order: .. code-block:: js async function createCompoundIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ lastName : -1, dateOfBirth : 1 }); console.log(result); return result; }; Create a Text Index ------------------- MongoDB also provides :manual:`text ` indexes to support text search of string content. Text indexes can include any field whose value is a string or an array of string elements. This example specifies a text index key for the ``comments`` field: .. code-block:: js async function createTextIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ comments : "text" }); console.log(result); return result; }; Create a Hashed Index --------------------- To specify a :manual:`hashed ` index key, use the ``hashed`` method. This example specifies a hashed index key for the ``timestamp`` field: .. code-block:: js async function createHashedIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ timestamp : "hashed" }); console.log(result); return result; }; Create Geospatial Indexes ------------------------- There are also helpers for creating the index keys for the various geospatial indexes supported by mongodb. Create a ``2dsphere`` Index ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To specify a :manual:`2dsphere ` index key, use one of the ``geo2dsphere`` methods. This example specifies a 2dsphere index on the ``location`` field: .. code-block:: js async function create2dSphereIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ location : "2dsphere" }); console.log(result); return result; }; Create a ``2d`` Index ^^^^^^^^^^^^^^^^^^^^^^^^^ To specify a :manual:`2d ` index key, use the ``geo2d`` method. .. important:: A 2d index is for data stored as points on a two-dimensional plane and is intended for legacy coordinate pairs used in MongoDB 2.2 and earlier. This example specifies a 2d index on the ``points`` field: .. code-block:: js async function create2dIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex({ points : "2d" }); console.log(result); return result; }; IndexOptions ------------ In addition to the index specification document, ``createIndex`` method can take an index options document, such as to create unique indexes or partial indexes. Create a Unique Index ^^^^^^^^^^^^^^^^^^^^^ .. code-block:: js async function createUniqueIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex( { lastName : -1, dateOfBirth : 1 }, { unique:true } ); console.log(result); return result; }; Create a Partial Index ^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: js async function createPartialIndex(db) { // Get the documents collection const collection = db.collection('users'); // Create the index const result = await collection.createIndex( { lastName : 1, firstName: 1 }, { partialFilterExpression: { points: { $gt: 5 } } }); console.log(result); return result; }; For other index options, see :manual:`Index Options ` . createIndexes ------------- The driver also supports the helper method ``createIndexes``. Unlike ``createIndex``, ``createIndexes`` takes in raw index specifications: .. code-block:: js async function createMultipleIndexes(db) { const collection = db.collection('users'); await collection.createIndexes([ // Simple index on field firstName { key: { firstName: 1 } }, // wildcard index { key: { '$**': 1 } }, // named index on lastName and firstName { key: { lastName: 1, firstName: -1 } name: 'lastNameReverseFirstName' } ]); }); You can find more info on raw index specifications :manual:`here ` .