SurrealDB for MongoDB developers
Quickly learn how to map your MongoDB knowledge to corresponding SurrealDB concepts and syntax.
Introduction
As a multi-model database, SurrealDB offers a lot of flexibility. Our SQL-like query language SurrealQL is a good example of this, where we often have more than one way to achieve the same result, depending on developer preference. In this mapping guide, we will focus on the syntax that most closely resembles the MongoDB query language (MQL).
Concepts mapping
For more in-depth explanations of SurrealDB concepts, see the concepts page.
MongoDB | 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 |
Syntax mapping
Let's get you up to speed with SurrealQL syntax with some CRUD examples.
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 COLUMNS 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.aggregate([ $group: { _id : "$name" } ]) | SELECT name FROM person WHERE name = "Jane" EXPLAIN | ||
db.person.aggregate([ $count: "personCount" ]) | SELECT count() AS person_count FROM person GROUP ALL | ||
db.person.find( name: "Jane", _id: 0, name: 1 ) | SELECT array::distinct(name) FROM person GROUP ALL | ||
db.person.find( name: "Jane", _id: 0, name: 1 ) | SELECT name FROM person WHERE name = "Jane" | ||
db.person.find().limit(10) | SELECT * FROM person LIMIT 10 | ||
db.review.aggregate([ "$lookup": { "from": "person", "localField": "person", "foreignField": "_id", "as": "person_detail" } ]) | 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 |