SQL and RethinkDB share very similar terminology. Below is a table of terms and concepts in the two systems.
SQL | RethinkDB |
---|---|
database | database |
table | table |
row | document |
column | field |
table joins | table joins |
primary key | primary key (by default id ) |
index | index |
This is a list of queries for inserting data into a database.
SQL | ReQL |
---|---|
INSERT INTO users(user_id, age, name) VALUES ("f62255a8259f", 30, Peter) | r.table("users").insert({ userId: "f62255a8259f", age: 30, name: "Peter" }) |
This is a list of queries for selecting data out of a database.
This is a list of commands for updating data in the database.
SQL | ReQL |
---|---|
UPDATE users SET age = 18 WHERE age < 18 |
r.table("users").filter( r.row("age").lt(18) ).update({age: 18}) |
UPDATE users SET age = age+1 |
r.table("users").update( {age: r.row("age").add(1)} ) |
This is a list of queries for deleting data from the database.
SQL | ReQL |
---|---|
DELETE FROM users |
r.table("users").delete() |
DELETE FROM users WHERE age < 18 |
r.table("users") .filter(r.row("age").lt(18)) .delete() |
This is a list of queries for performing joins between multiple tables.
SQL | ReQL |
---|---|
SELECT * FROM posts JOIN users ON posts.user_id = users.id |
r.table("posts").innerJoin( r.table("users"), function (post, user) { return post("userId").eq(user("id")); }).zip() Note: If you have an index (primary key or secondary index) built on the field of the right table, you can perform a more efficient join with eqJoin. r.table("posts").eqJoin( "id", r.table("users"), {index: "id"} ).zip() |
SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts JOIN users ON posts.user_id = users.id SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts INNER JOIN users ON posts.user_id = users.id |
r.table("posts").innerJoin( r.table("users"), function (post, user) { return post("userId").eq(user("id")); }).map({ postId: r.row("left")("id"), userId: r.row("right")("id"), name: r.row("right")("name") }) |
SELECT * FROM posts RIGHT JOIN users ON posts.user_id = users.id SELECT * FROM posts RIGHT OUTER JOIN users ON posts.user_id = users.id |
r.table("posts").outerJoin( r.table("users"), function (post, user) { return post("userId").eq(user("id")); }).zip() Note: You can perform more efficient r.table("posts").concatMap( function (post) { return r.table("users") .getAll(post("id"), {index: id}) .do( function (result) { return r.branch( result.count().eq(0), [{left: post}], result.map(function (user) { return { left: post, right: user }; }) ); } ); } ).zip(); |
SELECT * FROM posts LEFT JOIN users ON posts.user_id = users.id SELECT * FROM posts LEFT OUTER JOIN users ON posts.user_id = users.id |
r.table("users").outerJoin( r.table("posts"), function (user, post) { return post("userId").eq(user("id")); } ).zip() r.table("users").concatMap( function (user) { return r.table("posts").getAll(user("id"), {index: "id"}).do( function (results) { return r.branch( results.count().eq(0), [{left: user}], results.map(function (post) { return {left: user, right: post}; }) ); } ); } ).zip() |
This is a list of queries for performing data aggregation.
This is a list of queries for creating and dropping tables and databases.
Browse the following resources to learn more about ReQL:
© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/docs/sql-to-reql/javascript/