Skip to content

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:

json
{
  "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. maxDocuments only applies to capped collections.
  • validationLevel is off, moderate or strict, and validationAction is error or warn. A document the validator refuses answers 400 (database code 121), without the values of the document.
  • An existing collection is 409 (code 4091). Options that do not fit are 400 (code 4009).

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=true adds the statistics without host and namespace.
  • GET /mongodb/collections/info gives the numbers of all readable collections in one request (?database= for another database, ?name=a&name=b to restrict). A view is listed with available: 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.

RouteAnswer
GET .../fieldsthe field names
GET .../fields/detailone entry per path, with the types and how often each was found (tags[] for array elements)
GET .../schemaa JSON schema (draft 2020-12) of the plain format. Fields with several types use anyOf
GET .../schema/analysisthe analysis with counts and shares

With a sample, counts and shares are estimates and rare fields can be missing.

Rename and copy ​

json
// 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 (code 4091). dropTarget replaces 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. withIndexes recreates 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 as skippedIndexes ({ 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 .../indexindexes with keys as a map (legacy form)
GET .../index/detailsindexes with ordered keys, weights, collation, partial filter, TTL
GET .../index/statshow often each index was used since counting started
GET .../index/{name}one index (404, code 27, if missing)
PUT .../indexcreate an index
PUT .../index/field/{field}, /unique, /text, /{duration}/expiringshortcuts 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:

json
{
  "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.

Released under the MIT License.