Collection Indexes
🔧 Configuration
List the collection indexes
To list the collection indexes use the following request:
cURL
curl -X GET "[INSTANCE-URL]/inventory/_indexes" \
-H "Authorization: Basic [BASIC-AUTH]"
HTTPie
http GET [INSTANCE-URL]/inventory/_indexes \
Authorization:"Basic [BASIC-AUTH]"
JavaScript
fetch('[INSTANCE-URL]/inventory/_indexes', {
method: 'GET',
headers: {
'Authorization': 'Basic [BASIC-AUTH]'
}
})
.then(response => response.json())
.then(data => {
console.log('Retrieved collection indexes:', data);
})
.catch(error => console.error('Error:', error));
[
{
"v": 2,
"key": {
"_id": 1
},
"ns": "restheart.inventory",
"_id": "_id_"
}
]
Create an index
To create an index you have to specify the keys and the index options:
{ "keys": <keys>, "ops": <options> }
Example - create an unique, sparse index on property 'qty'
To create an unique, sparse index on property qty
run the following:
cURL
curl -X PUT "[INSTANCE-URL]/inventory/_indexes/qtyIndex" \
-H "Authorization: Basic [BASIC-AUTH]" \
-H "Content-Type: application/json" \
-d '{"keys": {"qty": 1},"ops": {"unique": true, "sparse": true }}'
HTTPie
http PUT [INSTANCE-URL]/inventory/_indexes/qtyIndex \
Authorization:"Basic [BASIC-AUTH]" \
Content-Type:application/json \
keys:='{"qty": 1}' \
ops:='{"unique": true, "sparse": true}'
JavaScript
fetch('[INSTANCE-URL]/inventory/_indexes/qtyIndex', {
method: 'PUT',
headers: {
'Authorization': 'Basic [BASIC-AUTH]',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"keys": {"qty": 1},
"ops": {"unique": true, "sparse": true}
})
})
.then(response => {
if (response.ok) {
console.log('Index created successfully');
} else {
console.error('Failed to create index:', response.status);
}
})
.catch(error => console.error('Error:', error));
Note
|
See also Indexes in MongoDB documentation https://docs.mongodb.com/manual/indexes/ |
Example - create a text index on property 'item'
To create a text index on property item
run the following:
cURL
curl -X PUT "[INSTANCE-URL]/inventory/_indexes/itemTextIndex" \
-H "Authorization: Basic [BASIC-AUTH]" \
-H "Content-Type: application/json" \
-d '{ "keys": { "item": "text" } }'
HTTPie
http PUT [INSTANCE-URL]/inventory/_indexes/itemTextIndex \
Authorization:"Basic [BASIC-AUTH]" \
Content-Type:application/json \
keys:='{"item": "text"}'
JavaScript
fetch('[INSTANCE-URL]/inventory/_indexes/itemTextIndex', {
method: 'PUT',
headers: {
'Authorization': 'Basic [BASIC-AUTH]',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"keys": { "item": "text" }
})
})
.then(response => {
if (response.ok) {
console.log('Text index created successfully');
} else {
console.error('Failed to create text index:', response.status);
}
})
.catch(error => console.error('Error:', error));
Delete an index
To delete an index use the following request:
cURL
curl -X DELETE "[INSTANCE-URL]/inventory/_indexes/qtyIndex" \
-H "Authorization: Basic [BASIC-AUTH]"
HTTPie
http DELETE [INSTANCE-URL]/inventory/_indexes/qtyIndex \
Authorization:"Basic [BASIC-AUTH]"
JavaScript
fetch('[INSTANCE-URL]/inventory/_indexes/qtyIndex', {
method: 'DELETE',
headers: {
'Authorization': 'Basic [BASIC-AUTH]'
}
})
.then(response => {
if (response.ok) {
console.log('Index deleted successfully');
} else {
console.error('Failed to delete index:', response.status);
}
})
.catch(error => console.error('Error:', error));
Notes
When creating an index the index options must be valid.
An example of invalid options is specifying the attribute unique on a property that is not actually unique; in this case the response will be 406:
HTTP/1.1 406 Not Acceptable
{
"_exceptions": [
{
"exception": "com.mongodb.DuplicateKeyException",
"exception message": "Write failed with error code 11000 and error message 'E11000 duplicate key error index: test.coll.$name2 dup key: ...."
}
],
"http status code": 406,
"http status description": "Not Acceptable",
"message": "error creating the index",
...
}
Indexes cannot be updated
To update an index, it must be deleted and recreated:
Trying to update an existing index returns 406 Not Acceptable.