Skip to content

Instantly share code, notes, and snippets.

View neumino's full-sized avatar

Michel neumino

View GitHub Profile
@neumino
neumino / gist:7310321
Last active December 27, 2015 10:19
For Gaddel, on IRC
r.table("users").filter( lambda user:
user["lst"].contains( lambda other_user:
other_user["name"] == "Bill"
)
)
r.db('test').table('test').map( function(doc) {
return r.expr(['id', 'value']).map( function(key) {
return r.expr([[key, r.expr([[doc(key).typeOf().default('UNDEFINED'), 1]]).coerceTo('OBJECT')]]).coerceTo('OBJECT')
}).reduce( function(left, right) {
return left.merge(right)
})
})
@neumino
neumino / gist:7109070
Created October 22, 2013 22:09
Multi index -- lexi2
// Data inserted
r.db('test').table('multi').insert({
id:1,
field: [
{key1: 1}, {key1: 2}, {key1: 3}
]
})
r.db('test').table('multi').insert({
id:2,
field: [
@neumino
neumino / gist:7063974
Created October 20, 2013 02:01
Return value in case of a one row update following a filter
r.db('test').table('test').filter(true)('id').limit(1).coerceTo('ARRAY').nth(0).do(function(idValue) {
return r.db('test').table('test').get(idValue).update({newField: "newValue"}, {returnVals: true})
})
r.db('test').table('test').filter(true)('id').limit(1).coerceTo('ARRAY').nth(0).do(function(idValue) {
return r.db('test').table('test').get(idValue).update({newField: "newValue"}, {returnVals: true})
})
r.db('etdemo').table('et_metrics').groupedMapReduce(
// group -- we return the value that we are going to use to group
function(doc) {
// We group by logTime rouned to 5*60 (5 min)
return doc("logTime").sub( doc("logTime").mod(5*60) )
},
// map -- we return the value we are interested in
// count is to keep track of how many group we are
function(doc) {
return {
r.table("t1").map( function(t1_doc) {
return t1_doc.merge({
names: t1_doc("names").map( function(name) {
return r.table("t2").get(name)("name")
})
})
})
@neumino
neumino / gist:6554108
Created September 13, 2013 18:11
Filtering nested arrays with RethinkDB
r.table("test").filter( function(doc) {
return doc("adresses").contains(function(adress) {
return adress("city").eq("Paris")
})
})
/*
{
@neumino
neumino / gist:6414163
Created September 2, 2013 15:35
For Gundrabur -- IRC 2013/09/02
r.db('test').table('test').filter({username:"Michel"})
.groupBy('origin', r.count)
.filter( r.row("reduction").gt(1))
.map(function(result) {
return r.db('test').table('test')
.filter({username:"Michel", origin: result("group")("origin")})
.coerceTo('array')
})
@neumino
neumino / gist:6393868
Created August 30, 2013 20:17
For RobertZ -- atomic update on condition + return result
r.db('test').table('test').get(1).update( function(doc) {
return r.branch(
doc("value").default(0).lt(0),
{field: "newValue"},
{}
)
}, {returnVals: true})