Reschema a collection

Give a collection a new structure: rename, retype, move, flatten, embed, extract or unwind fields, merge another collection, and write the result to new target collections.

Reschema reads a source collection or view and writes its documents, in a new shape, to one or more target collections. Configuring writes nothing. Execute writes the target collections only. The source, and any collection merged into it, are never written.

A new Reschema tab asking how to scan the source collection

Reschema a collection

Example: split acme_store.orders into orders_v2, with the line items in their own collection.

  1. Right-click orders and choose Reschema…. You can also use Collection › Reschema… or Reschema on the global toolbar.
  2. Pick a scan method (table below) to detect the source’s fields. The Reschema tab opens with one unit for orders.
  3. Click Add target collection, then choose an existing collection or name a new one (orders_v2). Tick the source fields it includes. _id is always included.
  4. Shape the target with the field tools: rename cust to customer_id, change total to Decimal128, and so on.
  5. Select items and choose Unwind selected array/object into a new collection order_items. Each element becomes a document, linked back by an items_foreignKey field.
  6. Click Preview to see five source documents and what each target receives.
  7. Set the Insertion Mode, then click Execute. If an existing target holds documents that would be overwritten, dropped or cleared, you are asked first.

Scan methods

MethodReads
Quick scanThe first 200 documents. Fast, but rare fields may be missed.
Full scanEvery document. Slower on a large collection, but complete.
CustomThe number of documents you set (1 to 10,000,000), from the first, the last or at random

Field tools

ToolEffect
Add new fieldA blank field, or a field taken from the source, optionally with a new type
Rename selected fieldA new name
Change typeA new BSON type, with a Fallback value for values that cannot be converted (not on the target _id)
Change source (field)Takes the value from another source field, or none
Delete selected fieldRemoves it from the target
Move selected field up / down / to parentReorders, or moves out of an embedded document
Embed selected fieldMoves fields into an embedded document
Flatten selected object/arrayLifts the sub-fields to the parent level
Extract selected field to a different collectionMoves the field to another target, linked by <field>_foreignKey in both
Unwind selected array/objectOne document per element in another collection, linked by <field>_foreignKey
Relate selected collection with anotherAdds <collection>_foreignKey to link two targets

Undo and Redo step through your changes, and the Log lists them.

Shape the source

  • Change source points the unit at another collection. Merges are removed and targets are kept.
  • Merge another collection brings in the documents of a lookup collection in the same database: choose the Local field, the collection to merge, its Foreign field and the target field. Matching documents are embedded.

Insertion modes

ModeEffect on an existing target
Overwrite documents with same _idReplaces documents with the same _id
Skip documents with existing _idLeaves them
Insert with new _id if _id existsInserts them with a new _id
Always insert with new _idInserts every document with a new _id
Drop collection first if it already existsDrops the target just before its first write
Clear collection first if it already existsEmpties the target just before its first write
Abort if _id already existsStops at the first existing _id; what was written stays

Several units

A Reschema tab can hold several units, one per source collection or view. Use Add unit and Remove unit in the Reschema overview. Execute runs the selected units, or all of them. Save task keeps the configuration to run again or on a schedule (Automation).

Tips

  • Write to new collections, check them, then rename. The source stays untouched, so you can run again at any time.
  • After a quick scan, rare fields may be missing. Rescan with Full scan before you execute.

Troubleshooting

ProblemWhat to do
“No field detected”The source holds no document, or the scan read too few. Rescan with more documents.
Execute is refused before the promptA target connection has the read-only lock, or a target is the source or a merged collection.
Values became the fallbackThey could not be converted to the new type. Check the Type breakdown of the field before you change its type.