Skip to content

Instantly share code, notes, and snippets.

View neumino's full-sized avatar

Michel neumino

View GitHub Profile
@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")
})
})
/*
{
r.table("test").get("idValue").update(function(doc) {
return r.branch(
doc("lastUpdate").eq(newDoc["lastUpdate"]),
newDoc.merge({lastUpdate: r.now()}),
r.error("Wrong version")
)
})
@neumino
neumino / gist:8363888
Created January 10, 2014 22:24
jakcharlton - IRC - 2014.01.10
// Retrieve all the documents where val == 1
// If no document is returned, insert {newVal: 2}
// Else update all the documents with {newVal: 2}
//
// Note: This query is not atomic
r.table("test").filter({val: 1}).count().do(function(numResults) {
return r.branch(
numResults.eq(0),
r.table("test").insert({newVal: 2}),
var rdbConn = {};
var r = require('rethinkdb');
if (r.protobuf_implementation !== 'cpp') console.log("WARNING: Wrong protobuf backend");
var async = require('async');
// Parameters
var numInserts = 5000;
// Global var
@neumino
neumino / gist:8285249
Created January 6, 2014 16:19
Ohmanous - Twitter - 2013.01.06
r.table("t1").eqJoin(
function(doc) { return doc("a")("b"); },
r.table("t2")
).map( function(left, right) {
return left.merge({
a: {
b: right
}
})
})
@neumino
neumino / gist:8210396
Last active January 1, 2016 22:29
nelish - IRC - 2013.01.01
r.db('shred').table("teams").map(function(team) {
return team.merge({
members: r.db('shred').table("users").filter(function(user) {
return team("members").contains(user("id"))
}).coerceTo("ARRAY")
})
})
// More efficient query that uses the index id
@neumino
neumino / gist:7938782
Last active December 31, 2015 05:09
emmo - IRC - 2013.12.12
conn = r.connect()
cursor = r.db('gbrl').table('race').get_all(14136, index='season_id') \
.pluck({'results':['driver', 'position']}, 'time') \
.order_by('time') \
.map( lambda race:
r.branch( # That's just a if
# Check if Andrew is in the results
race["results"]["driver"].contains("Andrew Vennaro"),
race["results"].filter(lambda doc:
@neumino
neumino / gist:7938008
Last active December 31, 2015 04:59
blazedd - IRC - 2013.12.12
r.table("users").filter( function(user) {
// map over all the keys of search
return r.expr(search).keys().map( function(key) {
// for each key, we return a boolean weather user(key) match search(key) (case insensitive)
return user(key).match( r.expr("(?i)").add(r.expr(search)(key)) ).ne(null)
}).reduce( function(left, right) { // Then for each boolean, we return left and right // so we perform a "and" operation here.
return left.and(right)
})
})
@neumino
neumino / gist:7916270
Last active December 31, 2015 01:49
ins0mnia -- IRC - 2013.12.11
r.db('todomvc').table('posts').map( function(post) {
return post.merge({
comments: r.db('todomvc').table('comments').filter( function(comment) {
comment("pid").eq( post("id") )
}).coerceTo("ARRAY")
})
})
// If you have an index on pid for the table comments, you can do
@neumino
neumino / gist:7903626
Created December 11, 2013 01:30
@bencevans - Twitter - 2013/12/10
r.db("ScrobbleGraph").table("scrobbles").groupedMapReduce(
function(doc) { return doc("date").date() },
function(doc) { return [doc] },
function(left, right) { return left.add(right)}
)
// It's better to use just an orderBy and group on the client because
// - The database won't have to keep everything in memory
// - It's faster because of the index