When thinking in a document model database, you will often find that the concepts align closely with SurrealDB. The table below maps common terms.
Concept mapping
| Document model | SurrealDB |
|---|---|
| database | database |
| collection | table |
| document | record |
| field | field |
| index | index |
| Objectid | record id |
| transactions | transactions |
| reference and embedding | record links, embedding and graph relations |
Benefits of using a document model database
Flexibility: You don’t need to define rigid schemas in advance. Changes to data structure are often just changes in the JSON object itself.
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.
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.
Easier horizontal scaling: Many document databases are built for horizontal partitioning (sharding), making them easier to scale for large workloads.
Using MongoDB syntax as a reference
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:
Syntax mapping
Create
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.
| MQL | SurrealQL |
|---|---|
| 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 |
Read
For more SurrealQL examples, see the SELECT, LIVE SELECT and RETURN pages.
| MQL | SurrealQL |
|---|---|
| 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 |
Update
For more SurrealQL examples, see the UPDATE page.
| MQL | SurrealQL |
|---|---|
| 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" |
Delete
For more SurrealQL examples, see the DELETE and REMOVE pages.
| MQL | SurrealQL |
|---|---|
| db.person.deleteMany({ name: “Jane” }) | DELETE person WHERE name = "Jane" |
| db.person.deleteMany({}) | DELETE person |
| db.person.drop() | REMOVE TABLE person |