Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active May 3, 2024 15:44
Show Gist options
  • Save bradtraversy/6aef077f93c48465891f12958b84a22d to your computer and use it in GitHub Desktop.
Save bradtraversy/6aef077f93c48465891f12958b84a22d to your computer and use it in GitHub Desktop.
RethinkDB Query Cheat Sheet

RethinkDB Cheat Sheet

Create database

r.dbCreate('mydb')

List databases

r.dbList()

Drop database

r.dbDrop('test')

Create table

r.db('mydb').tableCreate('users')

Delete table

r.db('mydb').table("test").delete()

Insert record

r.db('mydb').table('users').insert({
		name: 'Brad Traversy',
		city: 'Boston',
		age: 37
	}
)

Insert records (Array or object)

r.db('mydb').table('users').insert([
		{name: 'John Doe', city: 'Miami', age: 34},
		{name: 'Sara Wilson', city: 'Boston', age: 28},
		{name: 'Steve Smith', city: 'Detroit', age: 49},
		{name: 'Jen Williams', city: 'Miami', age: 35}
])

Get records

r.db('mydb').table('users')

Filter records

r.db('mydb').table('users').filter({
		city: 'Boston'
})

Select specific fields

r.db('mydb').table('users').pluck('name')

Order By

r.db('mydb').table('users').orderBy(r.desc('name'))

Pluck & order by

r.db('mydb').table('users').filter({
	'city': 'Boston'
}).orderBy(
	r.desc('name')
).pluck('name')

Limit records

r.db('mydb').table('users').limit(2)

Count

r.db('mydb').table("users").count()

Distinct

r.db('mydb').table("users").pluck("city").distinct()

Greater & less than

r.db('mydb').table("users").filter(r.row('age').gt(35))
r.db('mydb').table("users").filter(r.row('age').gte(35))
r.db('mydb').table("users").filter(r.row('age').lt(35))

Update record

r.db('mydb').table("users").filter({
		id: '592502cc-a84e-442d-a45f-24ebc344d8cf'
}).update({
    "city": 'Miami'
})

Delete record

r.db('mydb').table("users").filter({
		id: '592502cc-a84e-442d-a45f-24ebc344d8cf'
}).delete()

Joins

Create and insert data for second table

r.db('mydb').table('tasks')
r.db('mydb').table('tasks').insert([
		{user_id: '50ea168e-400f-4ad6-9e6e-a09451b9a837', text: 'Task One'},
		{user_id: 'f4fdffdc-30f7-4b4b-895d-c4a362864441', text: 'Task Two'},
		{user_id: '50ea168e-400f-4ad6-9e6e-a09451b9a837', text: 'Task Three'},
		{user_id: '50ea168e-400f-4ad6-9e6e-a09451b9a837', text: 'Task Four'}
])

Join tables

r.db('mydb').table("tasks").eqJoin("user_id", r.db('mydb').table("users")).zip()

zip() will merge the user in the task, overwriting fields in case of conflict

r.db('mydb').table("tasks").eqJoin("user_id", r.db('mydb').table("users")).zip()
@kururu-abdo
Copy link

good job

@reigelgallarde
Copy link

@bradtraversy Thanks for this. I'm learning a lot.

However, I'm a bit of stuck and would appreciate some help.

I'm looking into join tables, and can't really solve my problem.
What is the best way to get all users with counters of how many tasks they have?

Currently, I get all users and iterate it to count the number of tasks.
I think this is costly, not sure though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment