Collections & Indexes
Collections in the default database (CONNECTION_DATABASE) are addressed by name. Collections in another database are addressed as database:collection (geodata:locations) in every collection route, and grants use that full name.
Creating a collection
A collection is created by the first insert or index, or explicitly with PUT /mongodb/collections/{name} (administrate grant). The body is optional:
{
"capped": true, "sizeInBytes": 1048576, "maxDocuments": 1000,
"validator": { "$jsonSchema": { "bsonType": "object", "required": ["name"], "properties": { "name": { "bsonType": "string" } } } },
"validationLevel": "strict", "validationAction": "error",
"collation": { "locale": "en", "strength": 2 }
}- A capped collection needs
sizeInBytes.maxDocumentsonly applies to capped collections. validationLevelisoff,moderateorstrict, andvalidationActioniserrororwarn. A document the validator refuses answers400(database code 121), without the values of the document.- An existing collection is
409(code4091). Options that do not fit are400(code4009).
GET .../options answers the options. PATCH .../options changes validator, validationLevel and validationAction. The other options cannot be changed after creation.
Collection names
A name is refused with 400 (code 4007) if it is empty or longer than 120 characters, contains $ or control characters, starts with system. or the server prefix (mc_), or ends with .files / .chunks (bucket collections have their own routes).
Information
GET /mongodb/collections/{name}gives counts and sizes.?includeDetails=trueadds the statistics without host and namespace.GET /mongodb/collections/infogives the numbers of all readable collections in one request (?database=for another database,?name=a&name=bto restrict). A view is listed withavailable: false.
Fields and schema
These routes analyse a sample of 1000 documents by default. sampleSize=0 reads every document. depth (default 3) limits how deep objects are followed.
| Route | Answer |
|---|---|
GET .../fields | the field names |
GET .../fields/detail | one entry per path, with the types and how often each was found (tags[] for array elements) |
GET .../schema | a JSON schema (draft 2020-12) of the plain format. Fields with several types use anyOf |
GET .../schema/analysis | the analysis with counts and shares |
With a sample, counts and shares are estimates and rare fields can be missing.
Rename and copy
// POST .../rename
{ "target": "customers2024", "dropTarget": false }
// POST .../copy
{ "target": "customersBackup", "filter": "country:DE", "withIndexes": true, "dropTarget": false }- The target is in the same database.
- Rename needs administrate on the source and write on the target.
- Copy needs read on the source and write on the target.
- An existing target is
409(code4091).dropTargetreplaces it, which also needs administrate on the target. - A copy runs inside the database (no documents pass through the server) into a temporary collection that is renamed at the end. A failure leaves an existing target untouched.
withIndexesrecreates all indexes, including text, unique, partial and TTL indexes and collations. Since 2.8 the own indexes of a declaration (mongocamp_idx_*) are only copied to a declared target — on any other collection no index sync would manage them; the answer counts them asskippedIndexes({ name, count, indexes, skippedIndexes }).
Indexes
For the indexes of a declared collection use indexes in the declaration: the server creates and updates them at the start and manages only its own (mongocamp_idx_*). The routes below are for indexes made by hand; the server leaves those alone.
| Route | |
|---|---|
GET .../index | indexes with keys as a map (legacy form) |
GET .../index/details | indexes with ordered keys, weights, collation, partial filter, TTL |
GET .../index/stats | how often each index was used since counting started |
GET .../index/{name} | one index (404, code 27, if missing) |
PUT .../index | create an index |
PUT .../index/field/{field}, /unique, /text, /{duration}/expiring | shortcuts for single-field indexes |
DELETE .../index/{name} | drop |
Create an index with orderedKeys, since the order of a compound index matters. direction is 1, -1, text, 2dsphere, 2d or hashed:
{
"orderedKeys": [
{ "field": "category", "direction": "1" },
{ "field": "title", "direction": "text" },
{ "field": "body", "direction": "text" }
],
"indexOptionsRequest": { "name": "texts", "weights": { "title": 5, "body": 1 }, "defaultLanguage": "german" }
}The older map keys ({"a": 1, "b": -1}) still works; send one of the two. The options include unique, sparse, hidden, expireAfterSeconds, partialFilterExpression, weights, defaultLanguage and collation. An index request that cannot be valid is 400 (code 4006) and nothing is created. A duplicate key over existing data is 409.
