Coming from other databases

Coming from MongoDB

What maps from MongoDB to Raft DB (documents, filters, indexes), what does not (wire protocol, update operators, aggregation), and how to move a collection through JSON lines.

Raft DB stores documents: a key and a JSON object, in tables (the HTTP API also accepts the word collections). That part will feel familiar. The rest of MongoDB's surface is smaller here, and a MongoDB driver cannot connect: there is no MongoDB wire protocol.

Vocabulary#

MongoDBRaft DB
databasedatabase
collectiontable (collections is accepted in HTTP paths)
_idthe key (a string over HTTP)
documentdocument (a JSON object)
insertOne / replaceOne with upsertPOST .../documents with {"key":..., "document":...}
findOne({_id})GET .../documents/{key}
find(filter)POST /v1/query with a JSON filter
countDocuments(filter)POST /v1/query with "count": true
deleteOne({_id})DELETE .../documents/{key}
createIndexPOST .../documents/indexes
insertManyNDJSON import (.../import)

Filters look alike#

The filter language uses the operators you know: $eq $ne $gt $gte $lt $lte $in $nin $exists $not $and $or, dotted names for nested fields, and $text for text search. {"age": {"$gt": 30}, "city": "Pune"} means the same here.

One difference matters: types are strict. {"age": 30} matches an integer 30, not the double 30.0 and not the text "30". Check how your data was written (see the filter language).

SQL is available too: POST /v1/query with "language": "sql" accepts a SELECT over the table.

Example#

POST /v1/databases/shop/tables/orders/documents
{"key":"o-1001","document":{"customer":"Asha","total":120,"items":[{"sku":"A1","qty":2}]}}

POST /v1/query
{"db":"shop","table":"orders","text":"{\"total\":{\"$gte\":100}}","pageSize":50}

See HTTP: documents and HTTP: queries for the exact request bodies.

What is different or missing#

  • No update operators. There is no $set, $inc, $push. To change a field, read the document, change it in your code and write it back (existed: true tells you it replaced one). Two writers changing the same document at once can overwrite each other.
  • No aggregation pipeline. $group, $lookup, $project and the other stages do not exist. For grouping and joins, use the SQL front-ends (MySQL, PostgreSQL or SQL Server dialect) over the same engine, or do the work in the application.
  • No change streams, no GridFS, no geospatial or time-series collections.
  • Keys are strings. ObjectId, dates and decimals have no special type: store them as text or numbers and convert in your code.
  • Schemas are available and optional: a table can enforce a schema (documents and schemas).
  • Large result sets are read in pages with a cursor, not as a server-side cursor object.

Moving the data#

  1. Export each collection as JSON lines with MongoDB's own tools (mongoexport writes one document per line by default).
  2. Turn each line into {"key": <the _id as text>, "document": {...the rest...}}. A short script does this; extended JSON values such as {"$oid": "..."} or {"$date": "..."} must be converted to plain text or numbers first.
  3. Send the lines to POST /v1/databases/{db}/tables/{t}/import in batches (a body is under 1 MiB and at most 20,000 lines). The answer lists the lines that failed.
  4. Create the indexes you used, then compare counts: count through the API against countDocuments in MongoDB.

Do this on a copy, and keep MongoDB running until the application has been changed and tested.