createIndex
createIndex
is a Change Type in the Liquibase Open Source and Liquibase Pro extensions for MongoDB that creates an index on a collection.
Uses
The Liquibase Pro extension for MongoDB includes several modeled Change Types from the Liquibase Open Source version. These let you specify a few MongoDB commands using Liquibase XML, JSON, and YAML changelogs.
createIndex
is one such Change Type. You can use it to create an index on an existing collection in your database. This can help you run efficient queries on data that would otherwise be expensive to retrieve.
Note: If you want to specify mongosh
statements in your XML, JSON, and YAML changelogs, use the mongo
and mongoFile
Change Types instead.
Run
To run this Change Type, follow these steps:
- Add the Change Type to your changeset, as shown in the examples on this page.
- Specify any required attributes. Use the table on this page to see which ones your database requires.
- Deploy your changeset by running the
update
command:
liquibase update
Available attributes
For more information, see db.collection.createIndex().
Tip: You must specify all top-level attributes marked as required. If you specify an optional attribute, you must also specify any nested attributes that it requires.
Name | Type | Description | Requirement |
---|---|---|---|
collectionName
|
String | Name of the collection to create the index on | Required |
keys
Required. A document that defines the index's basic contents and structure.
keys
has the following nested attributes:
name
(required): the name of a key in the collection to use for the indextype
(required): the type of the index. For an ascending index, specify1
. For a descending index, specify-1
. Other accepted values includetext
,geospatial
,hashed
, and more.
options
Optional. A document that defines additional features for the index.
options
has the following nested attributes:
unique
(optional): iftrue
, creates a unique index so that the collection will not accept insertion or update of documents where the index key value matches an existing value in the index. Default:false
.name
(optional): the name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
Examples
databaseChangeLog:
- changeSet:
id: 3
author: your.name
changes:
- createIndex:
collectionName: countries_yaml
keys: '{ name: 1, type: 1}'
options: '{unique: true, name: "ui_countries"}'
{
"databaseChangeLog": [
{
"changeSet": {
"id": "3",
"author": "your.name",
"changes": [
{
"createIndex": {
"collectionName": "countries_json",
"keys": {
"$rawJson": {
"name": 1,
"type": 1
}
},
"options": {
"$rawJson": {
"unique": true,
"name": "ui_countries_json"
}
}
}
}
]
}
}
]
}
The Liquibase MongoDB Open Source extension uses a unique mongodb
XML namespace and XSD files in the changelog header. However, the ext
prefix used with other extensions is backwards-compatible:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongodb="http://www.liquibase.org/xml/ns/mongodb"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/mongodb
http://www.liquibase.org/xml/ns/mongodb/liquibase-mongodb-latest.xsd">
<changeSet id="3" author="your.name">
<mongodb:createIndex collectionName="countries">
<mongodb:keys>
{ name: 1, type: 1}
</mongodb:keys>
<mongodb:options>
{unique: true, name: "ui_countries"}
</mongodb:options>
</mongodb:createIndex>
</changeSet>
</databaseChangeLog>
Database support
This Change Type is only supported for MongoDB. It does not support auto rollback.