r.dbCreate('mydb')
r.dbList()
r.dbDrop('test')
r.db('mydb').tableCreate('users')
r.db('mydb').table("test").delete()
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}
])
r.db('mydb').table('users')
r.db('mydb').table('users').filter({
city: 'Boston'
})
r.db('mydb').table('users').pluck('name')
r.db('mydb').table('users').orderBy(r.desc('name'))
r.db('mydb').table('users').filter({
'city': 'Boston'
}).orderBy(
r.desc('name')
).pluck('name')
r.db('mydb').table('users').limit(2)
r.db('mydb').table("users").count()
r.db('mydb').table("users").pluck("city").distinct()
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))
r.db('mydb').table("users").filter({
id: '592502cc-a84e-442d-a45f-24ebc344d8cf'
}).update({
"city": 'Miami'
})
r.db('mydb').table("users").filter({
id: '592502cc-a84e-442d-a45f-24ebc344d8cf'
}).delete()
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'}
])
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()
@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.