Skip to main content
Version: 1.2.x

SurrealDB for MongoDB developers

MongoDB to SurrealDB mapping

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.

MongoDBSurrealDB
databasedatabase
collectiontable
documentrecord
fieldfield
indexindex
Objectidrecord id
transactionstransactions
reference and embeddingrecord 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.

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 COLUMNS name

Read

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": "person_detail" } }])SELECT *, person.name as reviewer FROM review

Update

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"

Delete

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