• Start

Document

Common patterns

Map document-database concepts to SurrealDB, compare SurrealQL with MongoDB-style operations, and find resources for CRUD and migration.

When thinking in a document model database, you will often find that the concepts align closely with SurrealDB. The table below maps common terms.

Document modelSurrealDB
database database
collection table
document record
field field
index index
Objectid record id
transactions transactions
reference and embedding record links, embedding and graph relations
  1. Flexibility: You don’t need to define rigid schemas in advance. Changes to data structure are often just changes in the JSON object itself.

  2. Natural data representation: Since you’re working with JSON-like objects, document databases align well with modern programming languages that manipulate data as objects or dictionaries.

  3. Simplicity of application code: Because you can embed everything related to an entity in a single document, you often have fewer JOINs (or complex queries) and simpler code for retrieving complete objects.

  4. Easier horizontal scaling: Many document databases are built for horizontal partitioning (sharding), making them easier to scale for large workloads.

As a multi-model database, SurrealDB offers a lot of flexibility. SurrealQL often provides more than one way to achieve the same result, depending on developer preference. The mapping below focuses on syntax that most closely resembles the MongoDB query language (MQL).

For SurrealQL equivalents to MongoDB data types and how to import MongoDB data into SurrealDB, see the Surreal Sync migration tool:

As MongoDB is schemaless, only the SurrealQL schemaless approach is shown below. For a schemafull option, see the DEFINE TABLE page.

For more SurrealQL examples, see the CREATE and INSERT pages.

MQLSurrealQL
db.createCollection("person") CREATE person
db.person.insertMany([{ name: "John" }, { name: "Jane" }]) INSERT INTO person [{name: "John"}, {name: "Jane"}]
db.person.createIndex({ name: 1 }) DEFINE INDEX idx_name ON TABLE person FIELDS name

For more SurrealQL examples, see the SELECT, LIVE SELECT and RETURN pages.

MQLSurrealQL
db.person.find() SELECT * FROM person
db.person.find({}, { _id: 0, name: 1 }) SELECT name FROM person
db.person.find({ name: “Jane” }, { _id: 0, name: 1 }) SELECT name FROM person WHERE name = "Jane"
db.person.find({ name: “Jane” }, { _id: 0, name: 1 }).explain() SELECT name FROM person WHERE name = "Jane" EXPLAIN
db.person.aggregate([{ $count: “personCount” }]) SELECT count() AS person_count FROM person GROUP ALL
db.person.aggregate([{ $group: { _id: “$name” } }]) SELECT array::distinct(name) FROM person GROUP ALL
db.person.find().limit(10) SELECT * FROM person LIMIT 10
db.review.aggregate([{ “$lookup”: { “from”: “person”, “localField”: “person”, “foreignField”: “id”, “as”: “persondetail” } }]) SELECT *, person.name as reviewer FROM review

For more SurrealQL examples, see the UPDATE page.

MQLSurrealQL
db.person.updateMany({ name: “Jane” }, { $set: { last_name: “Doe” } }) UPDATE person SET last_name = "Doe" WHERE name = "Jane"
db.person.updateMany({ name: “Jane” }, { $unset: { last_name: 1 } }) UPDATE person UNSET last_name WHERE name = "Jane"

For more SurrealQL examples, see the DELETE and REMOVE pages.

MQLSurrealQL
db.person.deleteMany({ name: “Jane” }) DELETE person WHERE name = "Jane"
db.person.deleteMany({}) DELETE person
db.person.drop() REMOVE TABLE person

Was this page helpful?